javascript - 如何在固定时间间隔后上下移动标题?

标签 javascript jquery

你能告诉我如何在规定的时间间隔后上下移动标题吗?这意味着我需要显示红色标题 3 秒,然后显示绿色标题 3 秒。我认为它将使用向上滑动和向下滑动jquery 的事件和更改 div 的顶部位置。

http://jsfiddle.net/HL5h5/

.redrect{
position: relative;
    top:0px;
    width:100%;
    background-color:red;
    height:100px


}
.greenrect{
   position:relative;
    top:200px
    width:100%;
    background-color:green;
    height:100px


}

最佳答案

如果您希望两者始终可见:

Fiddle

HTML:

<div class ="redrect"> </div>
<div class ="greenrect"> </div>

CSS:

.redrect{
    position  : relative;
    background: #c00;
    height    : 100px
}
.greenrect {
    position  : relative;
    background: #0a0;
    height    : 100px
}

JS:

function flip() {
    var x = parseInt($(".redrect").css('top')) ? 0 : 100,
        z = x ? [20, 10] : [10, 20];
    $(".redrect").animate({top: x, zIndex: z[0]}, 450);
    $(".greenrect").animate({top: -x, zIndex: z[1]}, 450);
}
setInterval(flip, 3000);

编辑:

自上而下的滑动:

Fiddle

HTML:

Lorem ipsum
<div id="wrap">
    <div class ="redrect">Eloha </div>
    <div class ="greenrect">Hi </div>
</div>

<button id="toggle">Stop</button>

CSS:

#wrap {
    height    : 100px;
    overflow  : hidden;
    position  : relative;
}
.redrect{
    position  : absolute;
    background: #c00;
    height    : 100px;
    width     : 100%;
}
.greenrect {
    position  : absolute;
    background: #0a0;
    height    : 100px;
    width     : 100%;
    top       : -100px;
}

JS:

function flipper(a, b, tw, ta) {
    this.a = a;   // Element 1
    this.b = b;   // Element 2
    this.t_anim = ta || 750;  // Animation time
    this.t_wait = tw || 3000; // Interval time
    this.tt = null;

    // Flip the two elements
    var flip = function($a, $b, time) {
        $a.css('zIndex', 10);
        $b.css('zIndex', 20).animate({
            top : 0
        }, time).
        promise().done(function() {
            $a.css('top', -100);
        });            
    };
    // Tick triggered by interval
    this.tick = function() {
        if (parseInt($(this.a).css('top'), 10))
            flip($(this.b), $(this.a), this.t_anim);
        else
            flip($(this.a), $(this.b), this.t_anim);
        return this;
    };
    // Toggle run/stop
    this.toggle = function() {
        if (this.tt !== null)
            this.stop();
        else
            this.start();
        return this.tt !== null;
    };
    // Stop
    this.stop = function() {
        clearInterval(this.tt);
        this.tt = null;
        return this;
    };
    // Start
    this.start = function() {
        this.stop();           // Make sure to stop it.
        this.tt = setInterval(
            $.proxy(this.tick, this),
            this.t_wait
        );
        return this;
    };
    return this;
}

var ff = new flipper(
                ".redrect", 
                ".greenrect", 
                1500, // Interval time
                750   // Animation time
            )
            .start();

$("#toggle").on("click", function(e){
    $(this).html(ff.toggle() ? "Stop" : "Start");
});

关于javascript - 如何在固定时间间隔后上下移动标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22242565/

相关文章:

javascript - Coldfusion 加载动画 cfform

JavaScript 挑战——Rails 按钮迭代

javascript - 在较小的屏幕上隐藏 JavaScript

javascript - Materialise Bootstrap 未被触发

javascript - 这些 POST 命令有什么区别?

javascript - 检测 jstree 中的复选框更改

javascript - 如何使用 Google 表格脚本从数组返回值

javascript - jQuery 函数在不应该运行的时候运行

javascript - Ajax 调用在 IE 和 Chrome 中有效(并且仅在 Firefox 中在 ajax 调用结束时在 Debug模式下添加断点时有效)

正则表达式 : Capture between two asterisks with multiple asterisks in comma delimited string