我有一个包含多个滑块的页面(来自http://www.switchonthecode.com/tutorials/using-jquery-slider-to-scroll-a-div)并且由于它们中的每一个都是不同的 div 我只是复制了代码和将其更改为独特的类,从而带来以下代码:
$(document).ready(function(){
$(".slider1").slider({
animate: true,
handle: ".handle1",
change: handleSliderChange1,
slide: handleSliderSlide1
});
$(".slider2").slider({
animate: true,
handle: ".handle2",
change: handleSliderChange2,
slide: handleSliderSlide2
});
$(".slider3").slider({
animate: true,
handle: ".handle3",
change: handleSliderChange3,
slide: handleSliderSlide3
});
});
function handleSliderChange1(e, ui)
{
var maxScroll = $(".gal1").attr("scrollWidth") - $(".gal1").width();
$(".gal1").animate({scrollLeft: ui.value * (maxScroll / 100) }, 1000);
}
function handleSliderSlide1(e, ui)
{
var maxScroll = $(".gal1").attr("scrollWidth") - $(".gal1").width();
$(".gal1").attr({scrollLeft: ui.value * (maxScroll / 100) });
}
function handleSliderChange2(e, ui)
{
var maxScroll = $(".gal2").attr("scrollWidth") - $(".gal2").width();
$(".gal2").animate({scrollLeft: ui.value * (maxScroll / 100) }, 1000);
}
function handleSliderSlide2(e, ui)
{
var maxScroll = $(".gal2").attr("scrollWidth") - $(".gal2").width();
$(".gal2").attr({scrollLeft: ui.value * (maxScroll / 100) });
}
function handleSliderChange3(e, ui)
{
var maxScroll = $(".gal3").attr("scrollWidth") - $(".gal3").width();
$(".gal3").animate({scrollLeft: ui.value * (maxScroll / 100) }, 1000);
}
function handleSliderSlide3(e, ui)
{
var maxScroll = $(".gal3").attr("scrollWidth") - $(".gal3").width();
$(".gal3").attr({scrollLeft: ui.value * (maxScroll / 100) });
}
虽然这也很好,但我有大约 7 张这样的幻灯片(上面只显示了 3 张),我对重复基本相同的代码感觉有点不好......
有没有办法简化这段代码?