悬停菜单后,Bootstrap轮播刹车

时间:2016-07-25 10:17:06

标签: javascript jquery css twitter-bootstrap

我在同一页面中使用Bootstrap轮播使用某种菜单效果有一种奇怪的行为。 基本上,如果我将鼠标悬停在菜单上,Bootstrap轮播刹车,并给我以下错误:

铬:

Cannot read property 'offsetWidth' of undefined

火狐:

    $next[0] is undefined

这是用于效果的Js 已更新代码:

   var marker = $('#marker'),
    current = $('.current');

// Initialize the marker position and the active class
current.addClass('active');
marker.css({
    // Place the marker in the middle of the border
    bottom: -(marker.height() / 2),
    left: current.position().left,
    width: current.outerWidth(),
    display: "block"
});


if (Modernizr.csstransitions) {
  console.log("using css3 transitions");
$('li.list').mouseover(function () {
    var self = $(this),
        offsetLeft = self.position().left,
        // Use the element under the pointer OR the current page item
        width = self.outerWidth() || current.outerWidth(),
        // Ternary operator, because if using OR when offsetLeft is 0, it is considered a falsy value, thus causing a bug for the first element.
        left = offsetLeft == 0 ? 0 : offsetLeft || current.position().left;
  // Play with the active class
    $('.active').removeClass('active');
    self.addClass('active');
    marker.css({
        left: left,
        width: width,
    });
});

// When the mouse leaves the menu
$('ul.UlList').mouseleave(function () {
  // remove all active classes, add active class to the current page item
    $('.active').removeClass('active');
    current.addClass('active');
  // reset the marker to the current page item position and width
    marker.css({
        left: current.position().left,
        width: current.outerWidth()
    });
});

} else {
console.log("using jquery animate");

$('li.list').mouseover(function () {
    var self = $(this),
        offsetLeft = self.position().left,
        // Use the element under the pointer OR the current page item
        width = self.outerWidth() || current.outerWidth(),
        // Ternary operator, because if using OR when offsetLeft is 0, it is considered a falsy value, thus causing a bug for the first element.
        left = offsetLeft == 0 ? 0 : offsetLeft || current.position().left;
  // Play with the active class
    $('.active').removeClass('active');
    self.addClass('active');
    marker.stop().animate({
        left: left,
        width: width,
    }, 300);
});

// When the mouse leaves the menu
$('ul.UlList').mouseleave(function () {
  // remove all active classes, add active class to the current page item
    $('.active').removeClass('active');
    current.addClass('active');
  // reset the marker to the current page item position and width
    marker.stop().animate({
        left: current.position().left,
        width: current.outerWidth()
    }, 300);
});
};

这是一个重新创建问题的codepen: http://codepen.io/florinsimion/pen/AXaaOK

1 个答案:

答案 0 :(得分:0)

这种情况正在发生,因为$('li')会匹配您网页上的所有li代码,包括轮播代码。您需要选择菜单的li标记:

$('ul > li').mouseover(....)
 //^ add UL as parent for all your events

更好的 解决方案将使用您的菜单的特定类:

current = $('.my-menu .current');
$('.my-menu li').mouseover(....);
$('ul.my-menu').mouseleave(....);
$('.my-menu .active').removeClass('active');

别忘了你的CSS,请看一下这个演示:http://codepen.io/anon/pen/AXaaap