mousemove和mouseenter左右平滑滚动和mouseleave功能

时间:2012-12-03 05:18:59

标签: javascript jquery

有没有人知道如何使这个动画更加顺畅地向左和向右滚动它使用$ .throttle类从github执行每毫秒它是否有人知道另一种方法可以更好地左右移动jsFiddle我也想要从包装器中做一个mouseleave和mouseout来将滚动内容设置为0px像素,我该怎么做?我的尝试失败了

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;
}
.item {
float: left;
margin-right: 5px;
list-style-type: none;
}

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(),
        limits = 50,
        center = wrapper_width / 2,
        wrapper_pos = wrapper.offset(),
        mousePos = Math.min(Math.max(e.pageX,wrapper_pos.left + limits), wrapper_pos.left+wrapper_width-limits);

    //calculate new left margin
    var tmp
    if(e.pageX > center) {
    tmp = (mousePos + 50 - 5) * (content_width - wrapper_width) / wrapper_width;
}else{
         tmp = (mousePos - 50) * (content_width - wrapper_width) / wrapper_width;

        }

    content.stop().animate({
        'margin-left': '-' + tmp + 'px'
    }, 'fast', 'easeOutSine');
}
}); 

1 个答案:

答案 0 :(得分:0)

使用向左滚动动画我觉得更好:

JS:

$(document).ready(function() {

    
    var wrapper = $('#wrapper'),
        content = $('#scroll-content'),
        width=0.
        wrapper_width=wrapper.outerWidth(),
        w_half=wrapper_width/2;
    var w_left=wrapper.position().left;
    //FIXME wait for images load, to get  width or calculate width other way!
    
    //calculate inner width;
    $('#scroll-content .item').each(function() {
        width+=$(this).width()+5;
    });
    content.css('width',width);
    //
    
    content_width = content.outerWidth();
    wrapper.bind('mouseenter mousemove drag', mousemove);
    function mousemove(e) {
        var mouse_pos=e.pageX-w_left;
        var content_pos=mouse_pos/wrapper_width*content_width-w_half;
        //console.log(e);
        wrapper.stop();
        wrapper.animate({scrollLeft: content_pos},300);
            
    }
});

fiddle

可能是某些浏览器不喜欢滚动而不是int或超出范围,但我相信你可以自己解决它