[英]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);
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.