简体   繁体   English

jQuery文本动画幻灯片

[英]jQuery text animation slide-in

I am trying to use jQuery slide-in animation and it seems to work fine, but my the second animation doesn't work. 我正在尝试使用jQuery滑入动画,但似乎工作正常,但是我的第二个动画不起作用。 Can anyone tell me what I am doing wrong? 谁能告诉我我在做什么错?

This line: 这行:

$('#headline1Txt').animate({'marginLeft': "100px"}, 1000);

is working fine, but this one: 工作正常,但是这个:

$("#headline1Txt").animate({left: "+=30"}, 500);

is not working. 不管用。

My Code 我的密码

HTML HTML

<div id="mainContainer">
    <div id="headlineText">
        <p id="headline1Txt" >Striped Bag</p>
    </div>
</div>

JS JS

$(document).ready(function () {
    $('#headline1Txt').animate({'marginLeft': "100px"}, 1000);
    $("#headline1Txt").animate({left: "+=30"}, 500);
});

CSS CSS

#headlineText {
     margin: 60px 80px;
}

The left CSS property specifies part of the position of positioned elements. 左侧的CSS属性指定定位元素的部分位置。 For absolutely positioned elements (those with position: absolute or position: fixed), it specifies the distance between the left margin edge of the element and the left edge of its containing block. 对于绝对定位的元素(那些具有位置:绝对或位置:固定的元素),它指定元素的左边缘边缘与其包含块的左边缘之间的距离。

https://developer.mozilla.org/en-US/docs/Web/CSS/left https://developer.mozilla.org/en-US/docs/Web/CSS/left

As the CSS left property is part of position:*; 由于CSS left属性是position:*;一部分position:*; property's, fix by adding the position property, so jquery knows what to increment a left value to. 属性,通过添加position属性来修复,因此jquery知道增加左值的方法。

In Action 行动中

CSS CSS

#headlineText
{
    margin:60px 80px;
}
#headline1Txt
{
    position:relative;
}

HTML HTML

<div id="mainContainer">

    <div id="headlineText">
        <p id="headline1Txt">Striped Bag</p>
    </div>

</div>

jQuery jQuery的

$(document).ready(function () {

   $('#headline1Txt').
       animate({ 'marginLeft': "100px" }, 1000).
       animate({ left:"+=30" }, 5000);

});

What are you trying to achieve? 您想达到什么目的?

You want the text to slide in (heading right across the screen) then slide another 30px? 您想让文字滑入(在屏幕上向右移动),然后再滑30像素吗?

Thats what $("#headline1Txt").animate({ 'marginLeft': "+=30" }, 500); 那就是$("#headline1Txt").animate({ 'marginLeft': "+=30" }, 500); will achieve. 将实现。 'marginLeft' not just left . 'marginLeft'不只是left

The left property of css only works with elements with absolute position. css的left属性仅适用于具有绝对位置的元素。 To make your animation work you have to put you element at absolute position. 为了使动画工作,必须将元素放置在绝对位置。

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

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