how to avoid flickering on mouseenter event in jquery?

拥有回忆 提交于 2019-12-23 02:38:25

问题


I don't know exact technical word for this but in following jquery code on 'mouseenter' and 'mouseleave' event the whole div get movement like button clicked (flickers?) and I also used 'mouseover' and 'mouseout' but same problem occurs.

$total_doctors=mysql_fetch_array(mysql_query("select count(*)  from doctor"));

Main div:

<div class='mainspan action-nav-button'>
    <a href='doctor.php'>
    <span>&nbsp;</span>
    <i class='fa  fa-user-md'></i>
    <span>Doctor</span>
    <span id='countdoctor'>0</span>
    </a>
</div>

Script code:

var docNumbr = <?php echo $total_doctors['0']; ?>;

$(document).ready(function(){
$({countNum: $('#countdoctor').text()}).animate({countNum: docNumbr}, {
duration: 2000,
easing:'linear',
step: function() {
$('#countdoctor').text(Math.floor(this.countNum)+1);
}
});
    $('#countdoctor').mouseenter(function(){
    $(this).text("Total Records: "+docNumbr).css({"opacity" : "0.5", "font-size" : "14px" });
    }).mouseleave(function(){
    $('#countdoctor').text(docNumbr).css({"opacity" : "1.0", "font-size" : "25px" });
});
});

I want to avoid resizing of div on mouseenter and on mouseleave.


回答1:


As you noticed it's because the font-size and the text change. It is not necessarily to just make the font sizes the same. I think your problem will be solved when countdoctor have a static height and width. e.g. :

#countdoctor
{
    display:block;
    height:50px; /*Toggle just how you like it*/
    width:100%; /*Depends on the parent, so just always take full width*/
}


来源:https://stackoverflow.com/questions/30817880/how-to-avoid-flickering-on-mouseenter-event-in-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!