javascript - 从右向左水平滚动

标签 javascript jquery css

JSFiddle for horizontal slide of divs

jQuery(function($) {
  $('a.panel').click(function() {
    var $target = $($(this).attr('href')),
        $other = $target.siblings('.active');

    if (!$target.hasClass('active')) {
        $other.each(function(index, self) {
            var $this = $(this);
            $this.removeClass('active').animate({
                left: $this.width()
            }, 500);
        });

        $target.addClass('active').show().css({
            left: -($target.width())
        }).animate({
            left: 0
        }, 500);
    }
  });
});

我这个 fiddle div 从左向右滑动。我需要的是反向。从右向左滑动。

最佳答案

您需要反转提供动画的函数。而不是

left: -($target.width())

我把下面的

left: -($(this).width() - ($target.width()))

这是我的可运行解决方案:

jQuery(function($) {

    $('a.panel').click(function() {
        var $target = $($(this).attr('href')),
            $other = $target.siblings('.active');
        
        if (!$target.hasClass('active')) {
            $other.each(function(index, self) {
                var $this = $(this);
                $this.removeClass('active').animate({
                    left: 0
                }, 500);
            });

            $target.addClass('active').show().css({
                left: -($(this).width() - ($target.width()))
            }).animate({
                left: 0
            }, 500);
        }
    });

});
#left, #right {
    position: relative;
    float: left;
    margin: 0 5px 0 0;
    border: 1px solid black;
    width: 200px;
    height: 300px;
    overflow: hidden;
}

div.panel {
    position: absolute;
    height: 100%;
    width: 100%;
    display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="left">
    <a href="#target1" class="panel">Target 1</a><br/>
    <a href="#target2" class="panel">Target 2</a><br/>
    <a href="#target3" class="panel">Target 3</a><br/>
</div>

<div id="right">
    <div class="panel" id="target1" style="background:green">Target 1</div>
    <div class="panel" id="target2" style="background:red">Target 2</div>
    <div class="panel" id="target3" style="background:yellow">Target 3</div>
</div>

关于javascript - 从右向左水平滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41479967/

相关文章:

javascript - 使用 RegExp、String 或其他方法确定类中声明的属性的值类型

javascript - Angular Directive(指令) : template from other file

javascript - 使用 "var that = this"了解 Javascript 作用域

javascript - Chrome 扩展程序中的视频事件记录

javascript - 根据用户年龄更改单选按钮值的问题

javascript - jquery datepicker突出显示两个日期之间的事件日期

javascript - 选择滚动元素时jsTree滚动回顶部

css - 宽度没有反应,因为它应该

asp.net - .net 下拉列表对齐文本

javascript - 单击菜单按钮,网站滚动到 div