简体   繁体   English

div在第一个和最后一个元素上的鼠标指针位置中心

[英]mouse pointer position center of div on first and last element

I came across this jquery example of animating a div to the left or right depending on position of the mouse on mouseenter or mousemove. 我遇到了这个jquery示例,该示例根据鼠标在mouseenter或mousemove上的位置向左或向右移动div。 It uses a jquery class called $.throttle which causes an execution of the code every milliseconds. 它使用一个名为$ .throttle的jquery类,它导致每毫秒执行一次代码。 I was wondering if someone new how to have the mouse pointer over the first and last div elements or over every div element to exactly in the center of each element so you don't have to have the mouse pointer right on the edge whe scrolling the element to the left or right. 我想知道是否有人想过如何将鼠标指针放在第一个和最后一个div元素上或每个div元素上,使其恰好位于每个元素的中心,因此您不必在滚动鼠标时将鼠标指针放在边缘上元素在左侧或右侧。 Basically I want it to have a limit that it can't go past aswell. 基本上,我希望它具有不能超过的极限。 You will see what im saying when you put you're mouse pointer over the example click here jsFiddle the example is done in jquery 1.5 当您将鼠标指针悬停在示例上时,您将看到即时消息在说什么,单击此处jsFiddle该示例在jquery 1.5中完成

HTML 的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: CSS:

#wrapper {
width: 100%;
height: 170px;
overflow: hidden;
background-color: red;
}
#scroll-content {
width: 1050px;
}

jquery: 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');
}
});

i hope understand you: 我希望了解你:

JS changes: 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 changes: CSS更改:

#wrapper {
    position: absolute;
    top:0;
    left:0
}
#scroll-content {
    position: relative;
}

Try: http://jsfiddle.net/karacas/RkbP6/181/ 尝试: http : //jsfiddle.net/karacas/RkbP6/181/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM