jQuery:滚动动态生成的列表项

发布于 2024-10-12 11:45:37 字数 1497 浏览 7 评论 0原文

我正在尝试将 Valums 的滚动脚本(http://valums.com/scroll-menu-jquery/)合并到 Cycle 插件的动态创建分页中,http://www.malsup.com/jquery/cycle/pager2.html。基本上我试图让动态分页(1、2、3...)在鼠标移动时滚动。

所以,我有原始的 Cycle 演示代码:

$('#slideshow').before('').cycle({ fx: '调低', 速度:'快', 超时:0, 寻呼机:'#nav',

// callback fn that creates a thumbnail to use as pager anchor 
pagerAnchorBuilder: function(idx, slide) { 
    return '<li><a href="#"><img src="' + slide.src + '" width="50" height="50" /></a></li>'; 
} 

});

然后我尝试应用:

$(function(){ //获取我们的元素以便更快地访问并设置覆盖宽度 var div = $('div#nav'), ul = $('ul#nav'), // 无序列表的左边距 ulPadding = 15;

//Get menu width
var divWidth = div.width();

//Remove scrollbars
div.css({overflow: 'hidden'});

//Find last image container
var lastLi = ul.find('li:last-child');

//When user move mouse over menu
div.mousemove(function(e){

  //As images are loaded ul width increases,
  //so we recalculate it each time
  var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;

  var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
  div.scrollLeft(left);
});

});

问题是:

var lastLi = ul.find('li:last-child');

找不到最后一个元素,因为所有元素都是动态创建的。有什么想法可以限制脚本,以便 #nav 中的元素可以在 mousemove 上滚动吗?

I am trying to incorporate Valums's scroll script (http://valums.com/scroll-menu-jquery/) to a dynamically created pagination of the Cycle plugin, http://www.malsup.com/jquery/cycle/pager2.html. Basically I am trying to get the dynamic pagination (1, 2, 3...) to scroll on mousemove.

So, I have the original Cycle demo code:

$('#slideshow').before('').cycle({
fx: 'turnDown',
speed: 'fast',
timeout: 0,
pager: '#nav',

// callback fn that creates a thumbnail to use as pager anchor 
pagerAnchorBuilder: function(idx, slide) { 
    return '<li><a href="#"><img src="' + slide.src + '" width="50" height="50" /></a></li>'; 
} 

});

And then I am trying to apply:

$(function(){
//Get our elements for faster access and set overlay width
var div = $('div#nav'),
ul = $('ul#nav'),
// unordered list's left margin
ulPadding = 15;

//Get menu width
var divWidth = div.width();

//Remove scrollbars
div.css({overflow: 'hidden'});

//Find last image container
var lastLi = ul.find('li:last-child');

//When user move mouse over menu
div.mousemove(function(e){

  //As images are loaded ul width increases,
  //so we recalculate it each time
  var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;

  var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
  div.scrollLeft(left);
});

});

The problem is that:

var lastLi = ul.find('li:last-child');

can not find the last element because all elements are dynamically created. Any ideas how the scripts could be bounded so the elements within #nav are scrollable on mousemove?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠檬 2024-10-19 11:45:37

我没有尝试你的代码,但无论如何 jQuery 都应该找到你动态创建的元素。

<div>
    <span>one</span>
    <span>two</span>
    <span>three</span>
    <span>four</span>
  </div>

<script>
    $("div span:last-child")
        .css({color:"red", fontSize:"80%"})
        .hover(function () {
              $(this).addClass("someclass");
            }, function () {
              $(this).removeClass("someclass");
            });

</script>

尝试动态地重新创建此列表。

聚苯乙烯
您的图像中缺少“alt”属性。

I didn't try your code out, but jQuery should find your dynamically created elements no matter what.

<div>
    <span>one</span>
    <span>two</span>
    <span>three</span>
    <span>four</span>
  </div>

<script>
    $("div span:last-child")
        .css({color:"red", fontSize:"80%"})
        .hover(function () {
              $(this).addClass("someclass");
            }, function () {
              $(this).removeClass("someclass");
            });

</script>

Try to recreat this list dynamically.

P.S.
You are missing the 'alt' attribute in your images.

不弃不离 2024-10-19 11:45:37

好吧,所以我必须使用 .live() ,如下所示:

div.live('mousemove', function(e) {
        var lastLi = ul.find('li:last-child');
        var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;
        var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
        div.scrollLeft(left);
    });

Ok, so I had to use .live() as shown below:

div.live('mousemove', function(e) {
        var lastLi = ul.find('li:last-child');
        var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;
        var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
        div.scrollLeft(left);
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文