简体   繁体   English

当尝试使用Jani动画时,div更改为错误的位置

[英]Div changes to wrong position when trying to animate with Jquery

I'm a bit new to Javascript and jQuery and I'm having trouble animating something correctly. 我对Javascript和jQuery有点陌生,无法正确设置动画效果。 I'm trying to make a <div> move just a few pixels to the left when hovering over something, but when you hover over the thing, the Div immediately moves all the way to the left side of the screen. 我试图将<div>悬停在某物上时仅向左移动几个像素,但是当您悬停在该物上时,Div会立即一直移动到屏幕的左侧。

Here the jQuery code: 这里是jQuery代码:

$(window).ready(function() {
    $(".article").hover(function() {
        $(".suddenlyNinjas").animate({"left": "-5px"}, "slow");
    });
});

Also here is the CSS of the div I'm trying to move: 这也是我要移动的div的CSS:

.suddenlyNinjas
{
    background-color:black;
    height:50px;
    width:125px;
    position:absolute;
    z-index:2;
    top:350px;
}

Animate uses it's absolute position if I recall. 我记得,Animate使用它的绝对位置。 So instead of moving it over -5 pixels from it's current position, it's moving it to -5px of the entire window. 因此,它不是将其从当前位置移到-5像素上,而是将其移到整个窗口的-5px。

Have you tried putting a div with position:relative around the suddenlyNinjas div? 您是否尝试过将div的位置:相对于忍者的div相对?

If that doesn't work, just subtract 5 from it's current position to get the new target position you want. 如果这不起作用,只需从当前位置减去5即可得到您想要的新目标位置。

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

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