如何使用javascript模拟css悬停按下按钮? (没有jquery)

时间:2014-04-15 11:19:08

标签: javascript html css hover mousehover

如何使用javascript模拟css悬停按下按钮? (PURE JS - NO JQUERY)

当" hover"在div" test"几个变化。我还要按或鼠标悬停一个模拟"悬停"在那个div。

示例:http://jsfiddle.net/yFgh5/ 更新:http://jsfiddle.net/yFgh5/6/

HTML:

<div class="test1">red to yellow</div>
<div class="test2">green to blue</div>
<div class="test3">text text text text text</div>
<input  type="button" onclick"simulate(document.getElementById('test1'), 'mouseover');" value="Submit" />

CSS:

.test1 { background:red; }
.test2 { background:green; }
.test3 { font-size:13px; }
.test1:hover { background:yellow; }
.test1:hover ~ .test2 { background:blue; }
.test1:hover ~ .test3 { font-size:20px; }

JAVASCRIPT:

function simulate(element, eventName)
{
    var options = extend(defaultOptions, arguments[2] || {});
    var oEvent, eventType = null;

    for (var name in eventMatchers)
    {
        if (eventMatchers[name].test(eventName)) { eventType = name; break; }
    }

    if (!eventType)
        throw new SyntaxError('Only HTMLEvents and MouseEvents interfaces are supported');

    if (document.createEvent)
    {
        oEvent = document.createEvent(eventType);
        if (eventType == 'HTMLEvents')
        {
            oEvent.initEvent(eventName, options.bubbles, options.cancelable);
        }
        else
        {
            oEvent.initMouseEvent(eventName, options.bubbles, options.cancelable, document.defaultView,
            options.button, options.pointerX, options.pointerY, options.pointerX, options.pointerY,
            options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, options.button, element);
        }
        element.dispatchEvent(oEvent);
    }
    else
    {
        options.clientX = options.pointerX;
        options.clientY = options.pointerY;
        var evt = document.createEventObject();
        oEvent = extend(evt, options);
        element.fireEvent('on' + eventName, oEvent);
    }
    return element;
}

function extend(destination, source) {
    for (var property in source)
      destination[property] = source[property];
    return destination;
}

var eventMatchers = {
    'HTMLEvents': /^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/,
    'MouseEvents': /^(?:click|dblclick|mouse(?:down|up|over|move|out))$/
}
var defaultOptions = {
    pointerX: 0,
    pointerY: 0,
    button: 0,
    ctrlKey: false,
    altKey: false,
    shiftKey: false,
    metaKey: false,
    bubbles: true,
    cancelable: true
}

困难在于此。 当&#34;鼠标悬停&#34;在一个按钮(divs结构全部下降)这应该模拟&#34; css悬停&#34;顶部div,在这种情况下&#34; test1&#34;。 也就是说,将鼠标悬停在按钮上并模拟&#34; css hover&#34;到&#34; test1&#34;

2 个答案:

答案 0 :(得分:1)

我希望我能正确理解你的观点。

<强> JS

var t = document.getElementsByClassName('test1')[0];
var b = document.getElementsByTagName('input')[0];
t.onmouseover = function(e){
    this.className += ' testHover';
}
t.onmouseout = function(e){
    this.className = this.className.replace(' testHover','');
}
b.onmouseover = function(e){
    var p = document.getElementsByClassName('test1')[0];
    p.className += ' testHover';
}
b.onmouseout = function(e){
    var p = document.getElementsByClassName('test1')[0];
    p.className = p.className.replace(' testHover','');
}

Working Fiddle

以上对象形式的代码:

var obj = {
    initialize: function () {
        this.t = document.getElementsByClassName('test1')[0],
        this.b = document.getElementsByTagName('input')[0]
        this.attachEvents();
    },
    addClass: function () {
        this.t.className += ' testHover';
    },
    removeClass: function () {
        this.t.className = this.t.className.replace(' testHover', '');
    },
    attachEvents: function () {
        var t = this.t;
        var b = this.b;
        t.onmouseover = this.addClass.bind(this);
        t.onmouseout = this.removeClass.bind(this);

        b.onmouseover = this.addClass.bind(this);
        b.onmouseout = this.removeClass.bind(this);
    }
};
obj.initialize();

<强> Working Fiddle

答案 1 :(得分:1)

为什么不模拟悬停只是添加(和删除)一个类?

您可以像这样定义一个类:

.test1:hover, .test1.hover {
    background:yellow;
}

然后根据需要添加和删除事件类。

但是,除非你遇到一些障碍,否则jQuery是更快的方法!