【问题标题】:jquery animate: two divs, one move out of screen, the other one move injquery animate:两个div,一个移出屏幕,另一个移入
【发布时间】:2011-02-18 02:07:25
【问题描述】:

我要实现一个动画,图片有两个div

D1 | D2

起初 D2 在屏幕中(100% 宽度),D1 是不可见的(在屏幕外)。 我想要一个动画,D2 向右移动,在屏幕外。 D1从左向右移动,最终占据屏幕(替换D1)。

如果您在注册用户时看到了 Groupon 的动画效果,您可能会明白我的意思...

谢谢。

【问题讨论】:

  • jquery animate 是你需要的,还有一点 css

标签: javascript jquery html animation


【解决方案1】:

编辑 好的.. 我想做一个通用的解决方案(通过动画包装页边距)。更清晰的代码和更可定制 => http://jsfiddle.net/steweb/rWbFw/

标记:

<div id="mask">
    <div id="wrapper">
        <div class="full" id="div1">hey there I'm the first div</div>
        <div class="full" id="div2">hey there I'm the second div</div>
        <div class="full" id="div3">hey there I'm the third div</div>
        <!-- add as many 'full' divs as you want -->
    </div>
</div>

css:

#mask{
    width:100%;
    overflow:hidden;
    position:relative;
}
#wrapper{
    width:100%;
    height:300px;  /* optional! */
}
.full{
    float:left;
    height:300px;  /* optional! */
}
#div1{
    background:green;
}
#div2{
    background:white;
}
#div3{
    background:red;
}

js:

var utils = {
    maskWidth : $('#mask').width(),
    currIndex : 0,
    setWidths : function(){
        //setting maskWidth
        utils.maskWidth = $('#mask').width();

        //setting wrapper width 
        $('#wrapper').css('width',$('.full').length * utils.maskWidth);

        //setting 'full div' width
        $('.full').each(function(index){
            $(this).css('width',utils.maskWidth);
        });

        //setting curr wrapper margin (for window resize)
        $('#wrapper').css('margin-left',-(utils.currIndex*utils.maskWidth));

    }
}

$('.full').click(function(){
    utils.currIndex = $(this).index()+1; //current elem index (for margin calc)
    if($(this).next().size() == 0){//if is the last, reset
        utils.currIndex = 0;
        $('#wrapper').animate({'margin-left':0});
    }else{ //animation, negative margin of the wrapper
        $('#wrapper').animate({'margin-left':-(utils.currIndex*utils.maskWidth)});
    }
});

$(window).resize(function() { //on resize, reset widths and margins
    utils.setWidths();
});

utils.setWidths(); //first time, set everything

-- 旧的--

您可以从以下内容开始:http://jsfiddle.net/steweb/dsHyf/

标记:

<div id="wrapper">
    <div class="full" id="div1"></div>
    <div class="full" id="div2"></div>
</div>

css:

#wrapper{
    width:100%;
    overflow:hidden;
    position:relative;
    height:300px;
}
.full{
    position:absolute;
    width:100%;
    height:300px;
}
#div1{
    background:#FF0000;
    left:0px;
}
#div2{
    display:none;
    background:#FFFF00;
}

js:

$('#div2').css('left',-$('#wrapper').width()).show();

$('#div1').click(function(){
    $(this).animate({'left':$('#wrapper').width()});
    $('#div2').animate({'left':0});
});

$('#div2').click(function(){
    $(this).animate({'left':-$('#wrapper').width()});
    $('#div1').animate({'left':0});
});

【讨论】:

  • 你好,斯图布!真是不错的发展。我很感激,因为它是在不依赖任何插件的情况下开发的。 &这也是我尝试过的,但未能成功!但是,您应该对此有所延迟吗?
  • 嗨@infoSetu..感谢您的喜欢..您想在哪里看到延迟?用户点击 => 延迟 => 滑动?
  • 当然可以。您可能已经看到我的代码,我在其中使用计时器插件设置时间。从左到右稍微移动,然后再回来。如果可以的话,我想这会让这更棒!谢谢。
【解决方案2】:

我已经用 jQuery 计时器插件尝试过这个,它在 localhost 上运行得很好。

您需要导入:jQuery & jQuery Timer Plugin

然后执行这个:

<script type="text/javascript" src="jquery-1.5.js"></script>
        <script type="text/javascript" src="jquery.timers-1.2.js"></script>
        <script type="text/javascript">
            var i=0,j=100;
            $('#1').css('width',"0%");
            $('#2').css('width',"100%");
            l2r();
            function l2r(){
                var i=0,j=100;
                $(document).everyTime(2, function() {
                    i+=0.10;
                    $('#1').css('width',i+"%");
                    j -= 0.10;
                    $('#2').css('width',j+"%");
                }, 1000);
                setTimeout("r2l()", 4000);
            }
            function r2l(){
                var i=100,j=0;

                $(document).everyTime(2, function() {
                    i-=0.10;
                    $('#1').css('width',i+"%");
                    j += 0.10;
                    $('#2').css('width',j+"%");
                }, 1000);
                setTimeout("l2r()", 4000);
            }

您的 HTML 将是:

        <div style='width:100%;'>
            <div id='1' style='background-color:#333333; height: 100px; float: left;'></div>
            <div id='2' style='background-color:#CCCCCC; height: 100px; float: right;'></div>
        </div>

如果我错了,请欣赏并纠正我。

编辑:看来问题仍然是不同的浏览器! Chrome 的超时时间应为 4000(设置为),而 Fire Fox 的超时时间应为 10000。让我稍后编辑代码以识别浏览器并设置超时。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-02
    • 2014-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    相关资源
    最近更新 更多