Search code examples
javascripthtmlcsshovermousehover

How to simulate css hover pressing a button with javascript? (no jquery)


How to simulate css hover pressing a button with javascript? (PURE JS - NO JQUERY)

When "hover" over the div "test" several changes. I want also to press or mouse hover a button that simulates the "hover" on that div.

Example: http://jsfiddle.net/yFgh5/ UPDATE: 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
}

The difficulty is this. When "mouse hover" on a button (divs structure is all down) this should simulate the "css hover" a top div, in this case "test1". That is, hover on button and simulate the "css hover" to "test1"


Solution

  • I hope I get your point correctly.

    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

    The above code in object form:

    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