像电子书网站一样的javascript鼠标时间延迟

标签 javascript

我需要鼠标悬停时间延迟的代码,例如 ebooks.com(畅销书标签), 这是我的 javascript 代码,一切都很好,但我需要时间延迟 onmouse over,

       var prevnum=0;
   var cate="cat1";
function mouseoverfn(id){

    document.getElementById("bestsellerin_book_"+id).style.display='block';
    prevnum!=id?document.getElementById("bestsellerin_book_"+prevnum).style.display='none':'';
    document.getElementById(cate).style.background='#ffffff';
    document.getElementById(cate).style.color='#000';
    document.getElementById(cate).style.posistion='absolute';
    mouseoutfn(id)
    prevnum=id;
}

function mouseoutfn(id){

    document.getElementById("bestsellerin_book_"+prevnum).style.display='none';
    document.getElementById("bestsellerin_book_"+id).style.display='block';
    document.getElementById("cat"+id).style.background='#94B83E';
    document.getElementById("cat"+id).style.color='#ffffff';


    cate="cat"+id;
}

HTML:

<div class="bestsellerin_txt_content" id="cat<?php echo $i; ?>" onmouseout="mouseoutfn(<?php echo $i; ?>)" onmouseover="mouseoverfn(<?php echo $i; ?>)">
    <span style="padding-left:4px;"><?php 
        echo substr(ucwords(strtolower($assigned_cat[$i]['Category']['name'])) , 0, 22);
        if( strlen($assigned_cat[$i]['Category']['name']) > 22){ 
            echo "..";
        }
    ?></span>
</div>

最佳答案

您可以在鼠标悬停功能中设置超时。如果用户 mouseout 在超时之前,超时将被取消。 (出于这个原因,您的 mouseover 和 mouseout 处理程序都需要能够访问超时,正如我在此处演示的那样)。

(function() {

    var mo_timeout;

    function mouseover() {
        mo_timeout = setTimeout(function() {
            //mouseover code here
        }, 1000);

    }

    function mouseoutfn(id){
        clearTimeout(mo_timeout);
        //mouseout code here
    }

})();

另外,您需要手动添加大量样式;添加一个应用样式的类怎么样?

关于像电子书网站一样的javascript鼠标时间延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11392706/

相关文章:

javascript - Visual Studio 2015 NodeJS/Typescript 覆盖 Node 启动文件

javascript - 用于自定义图像元素符号的 Nivo-Slider

javascript - 使用 HTML5 和 Javascript 播放声音的最佳方式

javascript - 如果成功或错误,则在不同的 div 中获取 ajax 结果

PHP echo 总是转到 JQuery $.ajax 错误 block

javascript - 使用 RSelenium/XML 抓取评论(用 javascript 生成)

javascript - jQuery 选择动态添加的表格行的 col-data

javascript - 从 rails 开始。对 SDK 插件等有建议吗?

JavaScript 或 jQuery 浏览器后退按钮点击检测器

javascript - 布局呈现后如何初始化 jQuery 对象?