开发者

Problem with centered menu with float:left and Javascript in IE 7 and IE9

I have made menu and centered it using css: margin-left:auto; margin-right: auto;. This menu is made of <a> links with float:left property. In Firefox and Chrome and even IE8 everything is ok - menu is in the center and in one horizontal line. But in IE9 there is problem - last element is 1px lower than rest of menu. It looks like that:

Problem with centered menu with float:left and Javascript in IE 7 and IE9

.

In IE7 there is also problem - last element is under the menu:

Problem with centered menu with float:left and Javascript in IE 7 and IE9

.

I have tried adding 开发者_运维知识库display:inline; and it helped with float - in IE7 and IE9 menu was also looking like it should be. But menu centering was lost - menu was in one line but it was starting from the left in all borwsers that I checked.

There is also Javascript that is adding fade effect to menu items. I think that problem lay within this Javascript.

Code of my menu looks like that:

And css:

    .menu
    {
        background: url('Images/bg.jpg') repeat-x left top;
        height: 73px;
        margin-top: 8px;
    }

    .menu_inner
    {
        margin: 0 auto;
        width: 1001px;       
    }

    .menu_inner a
    {
        float: left;
        padding: 0px;
        margin: 0px;
    }

And Javascript wchich is adding fade effect:

(function ($) {
    $.fn.cross = function (options) {
        return this.each(function (i) {
            // cache the copy of jQuery(this) - the start image
            var $$ = $(this);

            // get the target from the backgroundImage + regexp
            var target = $$.css('backgroundImage').replace(/^url|[\(\)'"]/g, '');

            // nice long chain: wrap img element in span
            $$.wrap('<span style="position: relative;"></span>')
            // change selector to parent - i.e. newly created span
                    .parent()
            // prepend a new image inside the span
                    .prepend('<img>')
            // change the selector to the newly created image
                    .find(':first-child')
            // set the image to the target
                    .attr('src', target);

            // the CSS styling of the start image needs to be handled
            // differently for different browsers
            if ($.browser.msie || $.browser.mozilla) {
                $$.css({
                    'position': 'absolute',
                    'left': 0,
                    'background': '',
                    'top': this.offsetTop
                });
            } else if ($.browser.opera && $.browser.version < 9.5) {
                // Browser sniffing is bad - however opera < 9.5 has a render bug 
                // so this is required to get around it we can't apply the 'top' : 0 
                // separately because Mozilla strips the style set originally somehow...                    
                $$.css({
                    'position': 'absolute',
                    'left': 0,
                    'background': '',
                    'top': "0"
                });
            } else { // Safari
                $$.css({
                    'position': 'absolute',
                    'left': 0,
                    'background': ''
                });
            }

            // similar effect as single image technique, except using .animate 
            // which will handle the fading up from the right opacity for us
            $$.hover(function () {
                $$.stop().animate({
                    opacity: 0
                }, 550);
            }, function () {
                $$.stop().animate({
                    opacity: 1
                }, 550);
            });
        });
    };

})(jQuery);

// note that this uses the .bind('load') on the window object, rather than $(document).ready() 
// because .ready() fires before the images have loaded, but we need to fire *after* because
// our code relies on the dimensions of the images already in place.
$(window).bind('load', function () {
    $('img.fade').cross();
});

Any help here much appreciated!


  1. If the black border are styles as well that width of the a + width of the border = width of the wrapper - otherwise it will not fit inside the wrapper div.

  2. If there is no border in css and if you have default the browser style e.g.

    * {margin: 0px; paddding: 0px;}
    

Then the problem could be that IE7 can't count! :) Change width: 1001px; to width: 1002px; and it will be fine!


There is clearly something wrong with this Javascript.

Try changing it for something else like: http://bavotasan.com/2010/jquery-simple-animated-fade-effect/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜