admin管理员组

文章数量:1433975

<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"><img src=".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>

The code above is working but when I move mouse pointer over the image in that div, it is flickering too much. I don't know why it is happening. What should be the change in the code that make it work properly?

DEMO Here

<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."><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>

The code above is working but when I move mouse pointer over the image in that div, it is flickering too much. I don't know why it is happening. What should be the change in the code that make it work properly?

DEMO Here

Share Improve this question edited Dec 31, 2013 at 3:45 Zentaurus 7782 gold badges12 silver badges27 bronze badges asked Dec 31, 2013 at 3:41 Arya MehtaArya Mehta 1511 gold badge3 silver badges13 bronze badges 1
  • Arya, check my solution. It works. Don't forget to +1 and select as solved. – Ali Gajani Commented Dec 31, 2013 at 3:58
Add a ment  | 

3 Answers 3

Reset to default 3

This is how you should have done it. Demo :http://jsfiddle/wUAGP/468/

Note adding 'left' : e.pageX+20 makes it even more smoother. Play around.

So, check out a cool .gif I made for you all. Interactive isn't it?

This is because of the gap between the .moveAble and the cursor, so they don't clash.

$(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' );

All you need to do is change 'left': e.pageX - (e.pageX/2) to just a static number like 10 or 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 (cleaned up and optimized for you):

.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 (added inline CSS for hiding and position):

<a href="www.google."><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 - use updated jQuery to avoid possible deprecation/removed ponents in the future.

Please add some margin between mouse pointer and moveable div, because when mouse inter within moveable div, a:hover not works and moveable div display bee "none".

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

本文标签: javascriptHow do I make smooth movement of div while moving it with mouse pointerStack Overflow