Delay CSS hovermouseover effect with JavaScript - Stack Overflow

I found a very nice image gallery script that uses only CSS here: The only thing keeping me from using

I found a very nice image gallery script that uses only CSS here: /

The only thing keeping me from using it is that the hover effect is too fast. I would like to delay the hover effect by about a second; meaning the image will not enlarge unless you mouseover for a second. How can I do this with JavaScript?

I found a very nice image gallery script that uses only CSS here: http://host.sonspring./hoverbox/

The only thing keeping me from using it is that the hover effect is too fast. I would like to delay the hover effect by about a second; meaning the image will not enlarge unless you mouseover for a second. How can I do this with JavaScript?

Share Improve this question asked Apr 24, 2011 at 15:22 ChadChad 2,4556 gold badges27 silver badges38 bronze badges 1
  • 2 You'd more-or-less end up rewriting the effect, since you need JavaScript for the delay, and the effect is implemented in pure CSS right now. – Matt Ball Commented Apr 24, 2011 at 15:30
Add a ment  | 

2 Answers 2

Reset to default 4

I imagine it can be done in these steps:

  1. Change the CSS provided such that the hidden elements (the "pop-up" images) don't set their display styles on hover. This should be simple enough, there probably aren't a lot of display specifications in the CSS.
  2. Create another CSS class which handles the display property (sets it to block, I imagine).
  3. Using JavaScript, add a handler to the mouseover event for the thumbnail elements. In that event, set your delay and then apply the new CSS class to the target element. (Don't forget to remove that class on mouseout.

I could probably tinker with this a bit to get some kind of proof of concept going if you need. It'll be fairly primitive, though. A few potential hang-ups may include:

  1. Will mouseover still apply when another element is displayed under the cursor? If not, will mouseout be raised or will these elements just stay open once opened?
  2. Selecting the proper element in each case may prove tricky. If we have to much around with HTML structure or element IDs/styles then there could be a good bit more work involved to update the existing CSS to match.

It's an interesting idea, so I'll be glad to give some attention to a prototype. But hopefully just the outline of what may be involved is enough to get you going :)

Edit: Ok, I was more interested in trying this than I thought. I have a prototype here. The hover effect is now handled by jQuery rather than pure CSS. The only thing I haven't added yet is the delay. I'm looking into it.

So far the only things I've changed are the addition of the JavaScript and removing the display property from .hoverbox a:hover .preview in the CSS. That's all.

In the interests of posterity on StackOverflow (and since I never entirely trust jsfiddle to keep things), here's the JavaScript code so far:

$(document).ready(function() {
    $('.hoverbox a').mouseover(function() {
        $(this).children('.preview').show();
    });
    $('.hoverbox a').mouseout(function() {
        $(this).children('.preview').hide();
    });
});

You could generally use CSS transitions, instead of using JavaScript. You also don't really need to use two images in HTML – you could enlarge images while hovering them and position them with negative margins. You also could get rid of the link tag. So, for a single item of the gallery, from this HTML:

<li> 
  <a href="#">
    <img src="img/photo01.jpg" alt="description" />
    <img src="img/photo01.jpg" alt="description" class="preview" />
  </a> 
</li>

You get to this:

<li>
  <img src="http://host.sonspring./hoverbox/img/photo01.jpg" alt="description" />
</li>

And in CSS you do something like this:

.hoverbox img {
    /* 
       in order to uze z-index you need 
       to have position:absolute or relative set
    */
    position:relative;
    background: #fff;
    border-color: #aaa #ccc #ddd #bbb;
    border:1px solid;
    padding: 2px;
    width: 100px;
    height: 75px;
    /*
      CSS3 transition shorthand order:
      transition: [transition-property] [transition-duration] [transition-timing-function] [transition-delay];
      if you don't specify values for transition-property, default is "all"
      if you don't specify values for transition-timing-function, default is linear
    */
    -webkit-transition:.01s 1s;
    -moz-transition:.01s 1s;
    -o-transition:.01s 1s;
    transition:.01s 1s;
}
.hoverbox img:hover {
    z-index:1;
    width:200px;
    height:150px;
    margin:-37px -50px;
    border-color:#000;
}

This won't work in non-modern browsers though :( namely: IE < 10

UPD: Here's what I put together: http://jsfiddle/gryzzly/JWk7V/3/

I've updated the script to include a script you could apply to handle this functionality with the same HTML and still use CSS3 with modern browsers: http://jsfiddle/gryzzly/JWk7V/4/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745311739a4622030.html

相关推荐

  • Delay CSS hovermouseover effect with JavaScript - Stack Overflow

    I found a very nice image gallery script that uses only CSS here: The only thing keeping me from using

    4小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信