jQuery水平滚动显示
我正在使用 jQuery 制作水平滚动条。我让它工作使用以下
var wrapper = $('#wrapper'),
content = $('#scroll-content');
wrapper.bind('mousemove', function(e){
var wrapper_width = wrapper.width(),
content_width = content.width();
//wrapper and content width
var tmp = e.pageX * (content.outerWidth() - wrapper.outerWidth()) / wrapper.outerWidth();
//calculate new left margin
content.css({ 'margin-left': '-'+tmp+'px' });
//set margin according
/*content.animate({
'margin-left': '-'+tmp+'px'
}, 30, 'easeOutSine');*/
});
每个 mousemove 事件我计算新的左边距,滑块跨越屏幕的 100% 宽度。
这可行,但我有两个问题。对每个鼠标移动事件调用计算似乎是不好的做法,因为有数百个事件。有没有更好的方法,也许使用计时器?
另一个问题,当用户第一次悬停滑块时,它会跳到位,我尝试使用 animate 以便滑块向下滑动到正确的位置,但似乎不起作用。 (在底部评论)
任何解决这些问题的帮助都会很棒。谢谢
I'm making a horizontal scroller using jQuery. I have it working using the following
var wrapper = $('#wrapper'),
content = $('#scroll-content');
wrapper.bind('mousemove', function(e){
var wrapper_width = wrapper.width(),
content_width = content.width();
//wrapper and content width
var tmp = e.pageX * (content.outerWidth() - wrapper.outerWidth()) / wrapper.outerWidth();
//calculate new left margin
content.css({ 'margin-left': '-'+tmp+'px' });
//set margin according
/*content.animate({
'margin-left': '-'+tmp+'px'
}, 30, 'easeOutSine');*/
});
Every mousemove event I calculate the new left margin, the slider spans 100% width of the screen.
This works, but I have two problems. It seems bad practise to be calling a calculation on every mousemove event as there are hundreds. Is there a better way, maybe using timers?
Another question, when the user first hovers the slider it jumps into place, I tried to use animate so that the slider would slide down to the correct position, but didn't seem to work. (commented at the bottom)
any help with these problems would be great. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 Ben Alman 的 Throttle Debounce 插件
然后执行如下操作
: :http://jsfiddle.net/petersendidit/RkbP6/1/
You can use Ben Alman's Throttle Debounce plugin
And then do something like this:
Example: http://jsfiddle.net/petersendidit/RkbP6/1/
我知道我在节流方面也见过类似的情况。 John Resig 发表了一篇有关 Twitter 无限滚动问题的博客。我在下面使用了他的解决方案。
这是任何感兴趣的人的博客文章。
http://ejohn.org/blog/learning-from-twitter/
I knew I had seen something similar in terms of throttling. John Resig posted a blog about problems with twitters infinite scroll. I used his solution in the following.
Here's the blog post for anyone interested.
http://ejohn.org/blog/learning-from-twitter/