简体   繁体   English

jQuery为浮动div设置动画

[英]jQuery animate a floating div

I'm trying to create a fade-in animation for a div. 我正在尝试为div创建淡入动画。 The idea is to move div from right to left and fade-in. 这个想法是将div从右移到左并淡入。 Because of the layout, the div is floated right above a certain window width (ie 501px) and left below (500px). 由于布局的原因,div浮动在某个窗口宽度(即501px)的上方,而在其下方(500px)。

HTML HTML

<div id="container">
    <div id="animated"></div>
</div>

CSS CSS

@media only screen and (min-width:501px)
{
    #animated {
        float:right;
    }
}

@media only screen and (max-width:500px) 
{
    #animated {
        float:left;
    }
}

When div is floated right everything works fine, but when floating property is set to left the movement stops working. 当div向右浮动时,一切正常,但是当float属性设置为left时,运动停止工作。 How can I change my script to achieve the movement with a unique code, independently from floating property and CSS breaking points? 如何独立于浮动属性和CSS断点,更改脚本以使用唯一的代码来实现运动? Thanks in advance. 提前致谢。

Fiddle 小提琴

You can use jQuery to get the browser window width and then do a conditional check on that for your animation. 您可以使用jQuery获取浏览器窗口的宽度,然后对动画进行条件检查。

Javascript: 使用Javascript:

// Returns float of animated window based on CSS values
var float = $('#animated').css('float');

if (float == 'right') {
    $('#animated').css("margin-right", '10%').animate({
        "margin-right": "-=10%",
        opacity: 1
    }, 2000, 'swing');
} else {
    $('#animated').css("margin-left", '10%').animate({
        "margin-left": "-=10%",
        opacity: 1
    }, 2000, 'swing');
}

DEMO http://jsfiddle.net/3L93z/3/ 演示http://jsfiddle.net/3L93z/3/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM