如何在用鼠标指针移动div的同时使div平滑移动?

时间:2013-12-31 03:42:00

标签: javascript jquery html

<style>
.moveAble {
    position: absolute;
    display:none;
}
a:hover + div.moveAble {
    display: block;
}
.moveAble .qtip {
background-color: #FFF;
background-color: rgba(255,255,255,.95);
border-color: #ccc;
padding: 10px;
-moz-box-shadow: 0 1px 4px rgba(0,0,0,0.2);
-webkit-box-shadow: 0 1px 4px rgba(0,0,0,0.2);
box-shadow: 0 1px 4px rgba(0,0,0,0.2);
}

.qtip-default {
border-width: 1px;
border-style: solid;
border-color: #f1d031;
background-color: #ffffa3;
color: #555;
}

.qtip, .qtip {
max-width: 280px;
min-width: 50px;
font-size: 10.5px;
line-height: 12px;
direction: ltr;
}
#nytmm .qtip-content {
border-color: #999;
color: #000;
padding: 4px 7px;
text-align: center;
}
.qtip-content {
position: relative;
padding: 5px 9px;
overflow: hidden;
text-align: left;
word-wrap: break-word;
}

.moveAble .qtip-content h5 {
font: 300 20px/22px "nyt-cheltenham",Georgia,"Times New Roman",serif;
color: #000;
margin: 0;
}

.moveAble .qtip-content h6 {
font: 300 13px/16px 'nyt-franklin',Arial,Helvetica,sans-serif;
margin: 0;
}
</style>

<a href="www.google.com"><img src="http://s.jeff.io/img/gifts.png" /></a>
<div class="moveAble">
    <div id="qtip-0" class="qtip qtip-default  qtip-pos-tr" style="z-index: 15001;">
        <div class="qtip-content" id="qtip-0-content">
            <h5>Dining Gifts »</h5>
                <h6>Pug Muddlers</h6>
        </div>
    </div>
</div>

<script> 
$(document).ready(function(){
      var $moveable = $('.moveAble');
      $(document).mousemove(function(e){
          $('.moveAble').css({'top': e.pageY,'left': e.pageX-(e.pageX/2)});
      });
   });
</script>

上面的代码工作正常,但是当我将鼠标指针移动到div的图像上时,它会闪烁太多。我不知道为什么会这样。代码中应该有哪些变化才能使其正常工作?

DEMO Here

3 个答案:

答案 0 :(得分:3)

您需要做的就是将'left': e.pageX - (e.pageX/2)更改为静态数字,例如10或5。

Pretty Demo :)

使用Javascript:

$(document).ready(function () {
    var $moveable = $('.moveAble');
    $(document).on({
        mouseenter: function () {
            $moveable.show();
            $(this).on('mousemove', function (evt) {
                var e = evt || window.event;
                $moveable.css({
                    top: e.pageY,
                    left: e.pageX + 5
                });
            });
        },
        mouseleave: function () {
            $moveable.hide();
        }
    }, 'a img');
});

CSS(为您清理和优化):

.qtip {
    max-width: 280px;
    min-width: 50px;
    font-size: 10.5px;
    line-height: 12px;
    direction: ltr;
    background-color: #FFF;
    background-color: rgba(255, 255, 255, .95);
    border-color: #ccc;
    padding: 10px;
    -moz-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2);
    -webkit-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2);
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2);
}
.qtip-default {
    border-width: 1px;
    border-style: solid;
    border-color: #f1d031;
    background-color: #ffffa3;
    color: #555;
}
.qtip-content {
    border-color: #999;
    color: #000;
    position: relative;
    padding: 5px 9px;
    overflow: hidden;
    text-align: left;
    word-wrap: break-word;
}
.qtip-content h5 {
    font: 300 20px/22px"nyt-cheltenham", Georgia, "Times New Roman", serif;
    color: #000;
    margin: 0;
}
.qtip-content h6 {
    font: 300 13px/16px'nyt-franklin', Arial, Helvetica, sans-serif;
    margin: 0;
}

HTML(为隐藏和位置添加了内联CSS):

<a href="www.google.com"><img src="http://s.jeff.io/img/gifts.png" /></a>
<div class="moveAble" style="display: none;position: absolute;">
    <div id="qtip-0" class="qtip qtip-default  qtip-pos-tr" style="z-index: 15001;">
        <div class="qtip-content" id="qtip-0-content">
                <h5>Dining Gifts »</h5>
                <h6>Pug Muddlers</h6>
        </div>
    </div>
</div>

PS - 使用更新的jQuery以避免将来可能的弃用/删除组件。

答案 1 :(得分:2)

这就是你应该怎么做的。演示:http://jsfiddle.net/wUAGP/468/

注意添加'left' : e.pageX+20会使其更加流畅。四处游玩。

所以,看看我为你们制作的很酷的.gif文章。互动不是吗?

enter image description here

这是因为.moveAble和光标之间存在差距,所以它们不会发生冲突。

$(document).ready(function() {
       $(document).hover(
            //Mouse-over    
            function(e) {
                $(this).mousemove(function(e) {
                $('.moveAble').css({
                    'position' : 'absolute',
                    'top' : e.pageY,
                    'left' : e.pageX+20    
                    }).show();
                });    
            },
            //Mouse-out
            function() {
                $('.moveAble').fadeOut(1300);
            }    
        );
    }, 'a img' );

答案 2 :(得分:1)

请在鼠标指针和可移动div之间添加一些边距,因为当鼠标在可移动div内时,a:悬停不起作用且可移动div显示变为“none”。

$(document).ready(function(){
      var $moveable = $('.moveAble');
      $(document).mousemove(function(e){
          $moveable.css({'top': e.pageY + 5,'left': e.pageX + 5});
      });
   });