简体   繁体   English

悬停时元素位置正在更改

[英]Element position is being changed on hover

I have a div with a text. 我有一个带文字的div。 I want to create a yellow circle background when hovering the div instead of the cursor. 我想在将div而不是光标悬停时创建一个黄色圆圈背景。

Here is what I got so far: JsFiddle Demo 这是到目前为止我得到的: JsFiddle演示

HTML: HTML:

<div id="board">
    <div id="ball"></div>
    Lorem ipsum dolor sit....
</div>

JQuery: jQuery的:

$(function(){
    $("#board").mousemove(function(e){
        var ball = $("#ball");
        var ballRadius = parseInt(ball.css("width"))/2;
        var parentOffset = $(this).offset(); 
        var relX = e.pageX - parentOffset.left - ballRadius;
        var relY = e.pageY - parentOffset.top - ballRadius;
        ball.css("top",relY + "px");
        ball.css("left",relX + "px");
        ball.css("display","inline-block");
    });
});

I manage to do it, but, when hovering, all my text is being shifted. 我设法做到了,但是,当悬停时,我所有的文字都被移动了。

position: relative对于#board添加,并将#ballposition: relative对于position: absolute更改。

The issue is your positioning. 问题是您的定位。 DEMO 演示

#board{
    position:relative;

#ball{
    position: absolute;

The reason is that relative positioning just lets you moving things around with top and left but the element still effects the layout and positioning of its siblings. 其原因是,相对定位只是让你感动的事情周围的topleft ,但仍因素影响它的兄弟姐妹的布局和定位。 Position, absolute ultimately does the same thing, but it removes that element from the layout, so it does not effect any of its siblings or other's positioning. 绝对位置最终会做同样的事情,但是它将元素从布局中删除,因此它不会影响其任何同级或其他元素的位置。

The other thing both relative and absolute positioning do is they make the element an offsetParent . 相对和绝对定位的另一件事是,它们使元素成为offsetParent In other words other absolute or relatively positioned elements contained within will be positioned based on that element. 换句话说,包含在其中的其他绝对或相对定位的元素将基于该元素进行定位。 Because of this you need to make the #board either relative or absolute, so that the positioning of #ball will be based on #board . 因此,您需要使#board相对或绝对,使#ball的位置将基于#board


PS. PS。 When storing references to a jQuery object var ball = $('#ball); 当存储对jQuery对象的引用时, var ball = $('#ball); its considered a good practice to put a $ on your variable. $放在变量上是一种很好的做法。 So... var $ball = $('#ball); 所以... var $ball = $('#ball); . That way other developers can easily tell what variables are actually jQuery objects. 这样,其他开发人员就可以轻松分辨出哪些变量实际上是jQuery对象。

You will want to set both the ball and the boards position to absolute 您将需要将球和板的位置都设置为absolute

#board{
    position:absolute;
    margin: 0 auto;
    margin-top: 50px;
    padding: 15px;
    width: 500px;
    height: 300px;
    border: 1px solid black;
    overflow: hidden;
    cursor: none;
    font-size: 20px;
}

#ball{
    position: absolute;
    width: 40px;
    height: 40px;
    background-color: yellow;
    border-radius: 100%;
    top: -50px;
    left: -50px;
    display: none;
    z-index: -1;
    /* transition: all 0.1s;
    -webkit-transition: all 0.1s;*/
}

http://jsfiddle.net/Zt7h3/18/ http://jsfiddle.net/Zt7h3/18/

I managed to change it up a bit. 我设法对其进行了一些修改。

http://jsfiddle.net/Zt7h3/19/ http://jsfiddle.net/Zt7h3/19/

var relY = e.pageY - parentOffset.top - ballRadius - $(this).height();

I put the ball div at the bottom, and then subtract the height of the div to determine current Y. 我将球div放在底部,然后减去div的高度以确定当前的Y。

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

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