JQuery:多个缩略图滚动条,无需 JavaScript 重复
我一直在研究缩略图自动滚动脚本。 谁能建议我如何拥有多个版本的滚动条而无需复制 JavaScript。如果我可以使代码通用,但以某种方式指定缩略图滚动条要在其中工作的 DIVS 的 ID(也许在变量中),那就太好了。非常感谢您的帮助! :)
这里它位于 JSFiddle 上。
JavaScript 如下所示:
$(function() {
buildThumbs();
function buildThumbs() {
$('div.album').each(function() {
var width = 0;
var wrapper = $(this).find('.st_thumbs_wrapper');
wrapper.find('.st_thumbs a').each( function() {
width += $(this).outerWidth(true);
});
var thumbs = $(this).find('.st_thumbs');
thumbs.css('width', width + 'px');
makeScrollable(thumbs, wrapper);
});
}
function makeScrollable(thumbs, wrapper) {
var width = wrapper.innerWidth();
wrapper.scrollLeft(0);
var leftBuffer = 200;
var rightBuffer = 200;
//When user move mouse over menu
wrapper.unbind('mousemove').bind('mousemove', function(e) {
var xPos = e.pageX - wrapper.offset().left - leftBuffer;
var xMax = wrapper.innerWidth() - rightBuffer;
if(xPos > 0 && xPos < xMax) {
var perc = xPos / (xMax - leftBuffer);
var scrollAmt = thumbs.outerWidth(true) - wrapper.innerWidth();
wrapper.scrollLeft(perc * scrollAmt);
}
});
}
});
I've been working on a thumbnail auto scrolling script.
Could anyone suggest how I could have multiple versions of the scroller without having to duplicate the JavaScript. It would be great if I could make the code generic, but somehow specify the ID's of DIVS in which the thumbnail scroller is to work, maybe in a variable. Help would be very much appreciated! :)
Here it is on JSFiddle.
Here is what the JavaScript looks like:
$(function() {
buildThumbs();
function buildThumbs() {
$('div.album').each(function() {
var width = 0;
var wrapper = $(this).find('.st_thumbs_wrapper');
wrapper.find('.st_thumbs a').each( function() {
width += $(this).outerWidth(true);
});
var thumbs = $(this).find('.st_thumbs');
thumbs.css('width', width + 'px');
makeScrollable(thumbs, wrapper);
});
}
function makeScrollable(thumbs, wrapper) {
var width = wrapper.innerWidth();
wrapper.scrollLeft(0);
var leftBuffer = 200;
var rightBuffer = 200;
//When user move mouse over menu
wrapper.unbind('mousemove').bind('mousemove', function(e) {
var xPos = e.pageX - wrapper.offset().left - leftBuffer;
var xMax = wrapper.innerWidth() - rightBuffer;
if(xPos > 0 && xPos < xMax) {
var perc = xPos / (xMax - leftBuffer);
var scrollAmt = thumbs.outerWidth(true) - wrapper.innerWidth();
wrapper.scrollLeft(perc * scrollAmt);
}
});
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 CSS 中删除绝对定位。工作正常。
http://jsfiddle.net/kFDMN/
如果你需要定位,那么你需要在一个额外的容器,或者在 jQ 中使用
wrapAll('
')
来动态添加它。Remove the absolute positioning from your CSS. Works fine.
http://jsfiddle.net/kFDMN/
If you need positioning, then you'll need to use it on an additional container, or use
wrapAll('<div>')
in your jQ to add it dynamically.