V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
soooulp
V2EX  ›  问与答

划词搜索脚本如何添加一个函数,判断鼠标放到图标栏不自动消失呢

  •  
  •   soooulp · 2020-05-04 15:34:02 +08:00 · 687 次点击
    这是一个创建于 1462 天前的主题,其中的信息可能已经有所发展或是发生改变。

    用油猴划词搜索脚本,划词会弹出图标栏,试了下使用 mouseIn 、mouseOver 函数,把鼠标移到图标栏的时候,都不能不自动消失,一定是哪里弄错了,感谢大佬指导
    完整代码
    浏览器搜索扩展工具
    原先是使用的 mouseIn,添加代码最前面,请问这一块要如何写呢

    mouseIn: 0;
    
        var TimeOutHide = function () {
            if (mouseIn === 0) {
                return fadeOut(icon);
            }
        };
    

    原脚本前面的图标栏的内容:
     var iconArray = [
            {
                name: '打开',
                image: 'https://i.ibb.co/PQ5xM2R/2-1.png',
                host: [''],
                popup: function (text) {
                    if(text.indexOf("http://")==0||text.indexOf("https://")==0)
                    window.open(text, "_blank");
                    else window.open("http://"+text, "_blank");
                }
    
            },
    

    然后在划词弹窗这样使用
    timer = window.setTimeout(TimeOutHide, 6000);
    



    第 1 条附言  ·  2020-05-04 19:58:35 +08:00

    改用原生的js实现mouseenter,提示Cannot read property 'relatedTarget' of undefined

        var hide_controls=function(e){
                            // console.log(this);	if(e.relatedTarget!==e.currentTarget&&e.currentTarget.getElementsByClassName(e.relatedTarget.className).length===0){
              icon.style.display = 'none';
             }
        }
    
        timer = window.setTimeout(hide_controls, 6000);
    
    第 2 条附言  ·  2020-05-06 16:08:20 +08:00

    不挪鼠标可以自动关闭、鼠标放icon上不关闭、快速移开鼠标可以自动关闭

    鼠标长时间放icon上,再移开又不自动关闭了,怎么解决呀

        var TimeOutHide;
        var ismouseenter = false;
    
        icon.onmouseenter = function(){
            console.log("ismouseenter");
            if(ismouseenter == true){ //已经移入直接返回
                return;
            } else {
                ismouseenter = true; // 状态设为移入
            }
        };
    
        icon.onmouseleave = function(){
            console.log("ismouseleave");
            if(ismouseenter == false){
                return;
            } else {
                ismouseenter = false;
            }
        };
    
        TimeOutHide = function () {
            if (ismouseenter == false) {
                return fadeOut(icon);
                console.log("doSomethingOk");
            }
        };
    
         fadeIn(icon);
    	
         clearTimeout(timer);
         timer = window.setTimeout(TimeOutHide, 6000);
    
    1 条回复    2020-05-07 10:08:08 +08:00
    soooulp
        1
    soooulp  
    OP
       2020-05-07 10:08:08 +08:00
    解决啦,在 icon.onmouseenter 、icon.onmouseleave 中分部加入 clearTimeout(timer);,清除定时就好了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2523 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 11:57 · PVG 19:57 · LAX 04:57 · JFK 07:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.