简体   繁体   English

对于> 1000px的值,向左动画图像不起作用

[英]Animating an image to the left is not working for a value >1000px

This is my .animate function 这是我的.animate函数

$(document).ready(function () {
    $('#slide').click(function () {
        $('#2guns').animate({
            left: 1000
        }, 10000);
    });
});

And this is the element I'm animating 这就是我正在制作动画的元素

<div class="content">
    <button id="slide">slide</button>
    <img src="/home/varun/Documents/Crown/images/line.png" id="2guns" STYLE="position:absolute;left:1293px;">
</div>

When I increase this: 当我增加这个:

 $('#2guns').animate({left: 1000},10000);

to

$('#2guns').animate({left: 2000},10000);

It starts moving to the right. 它开始向右移动。

I wan't to move the image beyond the left side of the screen. 我不会将图像移到屏幕的左侧。

Works fine, try to reduce to animation length to something more immediate like 1000 效果很好,请尝试将动画长度减少到更直接的值,例如1000

$('#guns').animate({
left: 2000
}, 1000);

http://jsfiddle.net/xWEPV/3/ http://jsfiddle.net/xWEPV/3/

It starts moving to the right. 它开始向右移动。

I wan't to move the image beyond the left side of the screen. 我不会将图像移到屏幕的左侧。

Then you would have to use negative coordinates, similar to the below: 然后,您将不得不使用负坐标,类似于以下内容:

$(document).ready(function () {
    $('#slide').click(function () {
        $('#2guns').animate({
            left: -1000 // should be at least the same of higher than the actual width of your image if you want it to go all the way to the left until it is gone
        }, 7000);
    });
});

DEMO - Moving the image to the left from the right 演示 -将图像从右侧向左移动


The top-left corner of your browser would be the (0, 0) coordinate. 浏览器的左上角将是(0, 0)坐标。

If you want to move something to the left until it is moving outside the screen you need to specify a negative value. 如果要向左移动某些东西直到它移出屏幕之外,则需要指定一个负值。 You also need to consider the width of the image if you want the image to completely disappear to the left. 如果希望图像完全消失在左侧,则还需要考虑图像的宽度。

For example if the image is 100px in length then you would have to specify at least -100 to ensure it is moved off-screen to the left. 例如,如果图像的长度为100px ,则必须至少指定-100才能确保将其移出屏幕向左移动。

Francois's answer is correct (would upvote, but I'm too much of a n00b here even to do that) - just to clarify it a bit. 弗朗索瓦(Francois)的答案是正确的(会赞成,但是我什至不愿意这样做)-只是为了澄清一下。 The CSS properties left, right, top and bottom refer to the distance in pixels (or ems or whatever) from the left, right, top and bottom of the containing element. CSS属性left,right,top和bottom指的是距包含元素的left,right,top和bottom的距离,以像素(或ems或其他值)为单位。

Positive values move the styled element further into the container element, away from the border; 正值将样式化元素从边界移到容器元素中; negative values move it in the opposite direction. 负值将其沿相反方向移动。

The odd behaviour in your code stems from the fact that your initial style tag has a left value of 1293px - putting the img 1293px to the RIGHT of the left edge of the containing element. 代码中的异常行为是由于您的初始样式标签的左侧值为1293px-将img 1293px放置在包含元素左边缘的RIGHT上。 When your initial jQuery handler sets left to 1000px, that moves it 293px to the left. 当您的初始jQuery处理程序将左设置为1000px时,会将其向左移动293px。 If you were to set it to 2000px, on the other hand, it would move the element 707px to the RIGHT. 另一方面,如果将其设置为2000px,它将707px元素移到RIGHT。 Hence the apparently 'inconsistent' behaviour. 因此,显然是“不一致”的行为。

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

相关问题 如何在1000px以下显示脚本 - how to script show in below 1000px 仅当视口宽度大于1000px时才运行WayPoint吗? - Only run WayPoints if viewport width is greater than 1000px? 滚动1000px后更改CSS类 - Change CSS class after scrolling 1000px down 如何禁用滚动条是身体的宽度和高度大于1000px和600px? - How to disable the scrollbars is width and height of body bigger than 1000px and 600px? 如何仅在 WordPress 中的视口大于 1000px 时执行一些 jquery 代码 - How to execute some piece of jquery code only when the viewport is above 1000px in WordPress 我想在1000px或更少的像素上触发此jQuery。 我可以用PHP做到吗? - I want to trigger this jQuery on 1000px or less. Can I do that with PHP? 当设备宽度小于1000px时,jQuery的.height()函数返回0 - jQuery's .height() function returns 0, when device width is less than 1000px 设备视口大于1000px时如何运行jQuery代码 - How to run jQuery code when the device viewport is greater than 1000px 如果窗口宽度在 1000px 下超过 0.2 秒,是否可以重新加载网站? - Is it possible to reload a website if the window width is for more than 0.2 seconds under 1000px? 如何为视口小于 1000px 且仅在主页上的设备运行 jQuery 代码 - How to run jQuery code for devices with a viewport of less than 1000px and only on the homepage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM