jQuery:滚动动态生成的列表项
我正在尝试将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我没有尝试你的代码,但无论如何 jQuery 都应该找到你动态创建的元素。
尝试动态地重新创建此列表。
聚苯乙烯
您的图像中缺少“alt”属性。
I didn't try your code out, but jQuery should find your dynamically created elements no matter what.
Try to recreat this list dynamically.
P.S.
You are missing the 'alt' attribute in your images.
好吧,所以我必须使用
.live()
,如下所示:Ok, so I had to use
.live()
as shown below: