Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulate Hover using jQuery

Tags:

jquery

css

hover

Given the existing "buttons"

HTML:

 <div id="MB">
    <ul class="list">
       <li id="post-5"><a href="#post-5">5</a></li>
       <li id="post-4"><a href="#post-4">4</a></li>
       <li id="post-3"><a href="#post-3">3</a></li>
       <li id="post-2"><a href="#post-2">2</a></li>
       <li id="post-1"><a href="#post-1">1</a></li>
    </ul> 
 </div>

CSS:

  #MB .list li a {
        float:left;
        color:#333;
        background:#f6f6f6;
        border:1px solid #eaeaea;
        border-right:none;
        padding:0 8px;
        }

   #MB .list li a:hover,
   #MB .list li a:focus {
        color:#fff;
        border:1px solid #333333;
        border-right:none;
        background:#404040;
        text-decoration:none;
        }

I'd like to simulate "hover" automatically on each button, sequentially, every n seconds.

This means that every n seconds a button is "hovered" (changing color etc), at next interval is "turned off" and the following button will "turn on" and so on...

like image 632
Riccardo Avatar asked Mar 02 '11 15:03

Riccardo


People also ask

How to make hover effect with jQuery?

The hover() is an inbuilt method in jQuery which is used to specify two functions to start when mouse pointer move over the selected element. Syntax: $(selector). hover(Function_in, Function_out);

How do you trigger a hover?

Trigger syntax is as follows <hover|hover(p)>:<effect> where: hover: - trigger effect on element :hover. hover(p|<selector>): - triggers the effect on child elements when parent element gets :hover.

How to call mouseover function in jQuery?

jQuery mouseover() MethodThe mouseover() method triggers the mouseover event, or attaches a function to run when a mouseover event occurs. Note: Unlike the mouseenter event, the mouseover event triggers if a mouse pointer enters any child elements as well as the selected element.

Can you use hover on a div?

To display div element using CSS on hover a tag: First, set the div element invisible i.e display:none;. By using the adjacent sibling selector and hover on a tag to display the div element.


2 Answers

I would use setInterval and jQuery.trigger('mouseover', …).

like image 111
kojiro Avatar answered Sep 29 '22 03:09

kojiro


#MB .list a:hover,
#MB .list a:focus,
#MB .list .active a {
  /* hover styles */
}

(I've simplified your selectors a bit, I would also suggest trying to remove the outer div as these are often unnecessary and the ul alone is enough)

Javascript hover:

function setHover() {
    if ($('#MB .list .active').next().length) {
        $('#MB .list .active').next().addClass('active').end().removeClass('active');
    } else {
        $('#MB .list .active').removeClass('active');
        $('#MB .list li:first-child').addClass('active');
    }
}

setInterval(setHover, 1000);
like image 40
roryf Avatar answered Sep 29 '22 02:09

roryf