我遇到了这个jquery示例,根据鼠标在mouseenter或mousemove上的位置,向左或向右设置div动画。它使用一个名为$ .throttle的jquery类,每隔几毫秒就会执行一次代码。我想知道是否有人新的如何将鼠标指针放在第一个和最后一个div元素上或每个div元素上正好在每个元素的中心,这样你就不必将鼠标指针放在边缘滚动元素向左或向右。基本上我希望它有一个它不能过去的限制。当你将鼠标指针放在示例上时,你会看到我在说什么单击此处jsFiddle示例在jquery 1.5中完成
HTML
<div id="wrapper">
<ul id="scroll-content">
<li class="item"><img src="http://placehold.it/100x150/09f" /></li>
<li class="item"><img src="http://placehold.it/100x150/2f2" /></li>
<li class="item"><img src="http://placehold.it/100x150/234" /></li>
<li class="item"><img src="http://placehold.it/100x150/342" /></li>
<li class="item"><img src="http://placehold.it/100x150/312" /></li>
<li class="item"><img src="http://placehold.it/100x150/131" /></li>
<li class="item"><img src="http://placehold.it/100x150/111" /></li>
<li class="item"><img src="http://placehold.it/100x150/222" /></li>
<li class="item"><img src="http://placehold.it/100x150/333" /></li>
<li class="item"><img src="http://placehold.it/100x150/122" /></li>
</ul>
CSS:
#wrapper {
width: 100%;
height: 170px;
overflow: hidden;
background-color: red;
}
#scroll-content {
width: 1050px;
}
jquery的:
$(document).ready(function() {
var wrapper = $('#wrapper'),
content = $('#scroll-content');
wrapper.bind('mouseenter mousemove', $.throttle(200, mousemove));
function mousemove(e) {
var wrapper_width = wrapper.outerWidth(),
content_width = content.outerWidth();
//calculate new left margin
var tmp = e.pageX * (content_width - wrapper_width) / wrapper_width;
content.stop().animate({
'margin-left': '-' + tmp + 'px'
}, 'fast', 'easeOutSine');
}
});
答案 0 :(得分:0)
我希望你能理解:
JS改变:
//exaggerate mouse-x
var exmousex = (e.pageX-100) * 1.3;
//calculate new left margin
var tmp = exmousex * (content_width - wrapper_width) / wrapper_width;
//LIMITS
if (tmp < 0) tmp = 0;
if (tmp > content_width - wrapper_width -5 ) tmp = content_width - wrapper_width -5;
content.stop().animate({'left': -tmp}, 'fast', 'easeOutSine');
CSS更改:
#wrapper {
position: absolute;
top:0;
left:0
}
#scroll-content {
position: relative;
}