【发布时间】:2012-02-15 16:22:23
【问题描述】:
我正在尝试使用 JqueryTools 的 rangeinput 函数替换浏览器的本机滚动条。
这里是示例页面:http://conceptionkit.co.uk/ck/support
页面有多个可滚动的 div,其 html 结构如下:
<div class="scrollwrap"><!-- scrollwrap doesn't move and has overflow:hidden -->
<div class="scroll"><!- this is the scrollable pane --></div>
<!-- this is the rangecontrol that is converted into a
vertical scrollbar by the script -->
<input class="hide" type="range" max="300" step="1" />
</div>
对于页面上的单个实例,以下代码有效:
var scroll = jQuery(".scroll");
// initialize rangeinput
jQuery(":range").rangeinput({
// slide the DIV along with the range using jQuery css() method
onSlide: function(ev, step) { scroll.css({bottom: -step});},
// display progressbar
progress: true,
// initial value. also sets the DIV initial scroll position
value: 100,
// this is called when the slider is clicked. we animate the DIV
change: function(e, i) { scroll.animate({bottom: -i}, "fast");},
// disable drag handle animation when when slider is clicked
speed: 0
});
但是,当我尝试概括脚本时,它不起作用。我认为问题在于有多个名为“scroll”的变量实例。也许他们有冲突?
这是我尝试在页面上的所有滚动包装实例上放置滚动条的脚本。由于我必须将每个范围输入关联到正确的窗格,因此它遍历 jQuery(".scrollwrap").each() 并在每个窗格中找到滚动窗格和范围控件:
// default style for .scroll is overflow:auto for no-js browsers.
// must turn that off first
jQuery(".scroll").css('overflow','hidden');
jQuery(".scrollwrap").css('overflow','hidden').each(function(index){
// Inject range control element
jQuery(this).append('<input class="hide" type="range" max="300" step="1" />');
var scroll = jQuery(".scroll",this);
// initialize rangeinput
jQuery(":range", this).rangeinput({
// slide the DIV along with the range using jQuery css() method
onSlide: function(ev, step) { scroll.css({top: -step});},
// display progressbar
progress: true,
// initial value. also sets the DIV initial scroll position
value: 100,
// this is called when the slider is clicked. we animate the DIV
change: function(e, i) { scroll.animate({top: -i}, "fast");},
// disable drag handle animation when when slider is clicked
speed: 0
});
});
上面的脚本不会产生任何错误,它确实创建了滑块控件,但是移动幻灯片并不会移动相关的.scroll div。
我在这里做错了什么?
【问题讨论】:
标签: jquery html scrollbar each jquery-tools