【问题标题】:mousemove and mouseenter smooth scrolling left and right and mouseleave functionmousemove 和 mouseenter 平滑左右滚动和 mouseleave 功能
【发布时间】:2012-12-03 05:18:59
【问题描述】:

有谁知道如何让这个动画左右滚动更顺畅吗?它使用 $.throttle 类,每毫秒执行一次它从 github从包装器中执行 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');
}
}); 

【问题讨论】:

  • 你知道如果你添加另一个li它不会显示?这是预期的?
  • 我认为你可以减少 200 的值。
  • 你只需改变滚动内容 div 的大小

标签: javascript jquery


【解决方案1】:

使用向左滚动动画感觉更好:

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 或超出范围,但我相信你可以自己修复它

【讨论】:

  • 感谢您的回复,您的样品效果很好。每个元素在滚动内容中的大小都相同 我在示例中添加了一个 if 语句,将元素宽度/2(.item 类)划分为 50,因此当您将鼠标悬停和鼠标移动时,左右有限制50 像素是一个边界,您可以重新编码我所做的事情,以便它与您的示例一起正常工作
  • 我刚刚添加了边界来测试它是否正确处理了不在屏幕左侧的小部件。你要我编辑答案吗?您可以编辑答案,如果同意,我会同意
  • 我不明白 devide by 2 的想法,它应该是什么感觉?
  • 我的想法是,根据用户行为计算动画速度,例如他试图更慢地移动鼠标,以精确点击图像,但我不知道如何计算公式,我尝试了简单,它只是让一切都在跳跃
猜你喜欢
  • 1970-01-01
  • 2023-03-14
  • 2011-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多