javascript - 如何在用户脚本中获得鼠标悬停图像效果?

标签 javascript greasemonkey

我需要在用户脚本中获得图像的翻转效果,但它不起作用。

这是我尝试过的:

/*--- Create a button in a container div.  It will be styled and positioned with CSS.
*/
var zNode       = document.createElement ('input');
zNode.setAttribute ('id', 'suButton');
zNode.setAttribute( 'type', 'image' );
zNode.setAttribute( 'src', 'http://www.creativeadornments.com/nephco/doraemon/icons/doraemon_06.gif' );
znode.onmouseover='mouseover()';
znode.onmouseout='mouseOut()';
document.body.appendChild (zNode);

function mouseOver()
{
document.getElementById("suButton").src ="http://www.creativeadornments.com/nephco/doraemon/icons/doraemon_07.gif";
}

function mouseOut()
{
document.getElementById("suButton").src ="http://www.creativeadornments.com/nephco/doraemon/icons/doraemon_06.gif";
}

//--- Activate the newly added button.
document.getElementById ("suButton").addEventListener ("click", ButtonClickAction, true);

function ButtonClickAction (zEvent)
{
    //--- For our dummy action, we'll just add a line of text to the top of the screen.
    var button  = document.createElement ('a');
    location.href='http://www.stumbleupon.com/to/stumble/stumblethru:'+location.href.replace("http://","").replace("https://","").replace("ftp://","").split('/',4)[0];
}

//--- Style our newly added elements using CSS.
GM_addStyle ( (<><![CDATA[
    #suButton {
        position:               fixed;
        bottom:                 0px;
        left:                   0px;
        margin:                 0px 0px 50px 0px;
        opacity:                0.4;
        cursor:                 url(C:\buttercup_06.cur),url(http://www.creativeadornments.com/nephco/powerpuffgirls/cursors/ppg_01anim.gif),url(myBall.cur),pointer;
        border:                 0px outset red;
        z-index:                222;
        padding:                5px 5px;
    }
]]></>).toString () );

最佳答案

删除这些行:

znode.onmouseover='mouseover()';
znode.onmouseout='mouseOut()';

这不是添加事件监听器的方式。

然后改变这个:

//--- Activate the newly added button.
document.getElementById ("suButton").addEventListener ("click", ButtonClickAction, true);

为此:

//--- Activate the newly added button and add rollover image handling.
var zNode = document.getElementById ("suButton");
zNode.addEventListener ("click",        ButtonClickAction,  true);
zNode.addEventListener ("mouseover",    mouseOver,          true);
zNode.addEventListener ("mouseout",     mouseOut,           true);

关于javascript - 如何在用户脚本中获得鼠标悬停图像效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8982545/

相关文章:

javascript - 除非刷新页面,否则 Grease/tampermonkey 脚本无法正常工作

javascript - HTML 表格样式特定列

javascript - 渲染 Prop 和 React Router

javascript - 从 ASP.NET 中的按钮播放声音

javascript - 为什么 Firefox 会失去对页面重新加载的关注?

jquery - Greasemonkey、跨域、jQuery、get() 请求失败,出现 405 "method not allowed"

javascript - 从 ionic 模板调用 Controller 中的 javascript 函数

javascript - GM_setValue 和 GM_getValue 的 IE 等效项(Greasemonkey 存储)

javascript - 如何自动选择照片单选按钮?

javascript - GreaseMonkey,更改一些js函数