2

This is a problem I'm having in Chrome. It does not happen in Firefox. I will provide CSS, HTML, and Jquery Examples at the bottom.

The problem: When I hover over a PNG (which happens to be in a carousel jquery plugin right now), a hover image replaces the initial PNG. It even happens when I drag an element with Jquery's drag and drop functionality. It works without any problems, but the background slightly shifts or becomes distorted for just a second when it happens. It's not a great user experience, and I was wondering if someone knew how to fix it. Let me know what code you need to look at.

Code Examples

HTML:

<li id="img-home"><img id="img-home-src" src="<?php echo base_url();?>files/assets/images/homepage/img.png" alt="" /></li>

CSS:

li {
    text-align: center;
    cursor: pointer;
}

#img-home
{
    border:0;
    width:386px;
    height:484px;
    overflow:hidden;
    display: inline-block;
    padding: 0 0 0 0;
    margin: 0 0 0 0;
}

#img-home-src
{
    padding: 0 0 0 0;
    margin: 0 0 0 0;border:0;

}

Jquery:

       $("#img-home").hover(
    function () {
          $("#img-home-src").attr("src","<?php echo base_url();?>files/assets/images/homepage/img_hover.png");
    }, 
    function () {
          $("#img-home-src").attr("src","<?php echo base_url();?>files/assets/images/homepage/img.png");
    }
  );
willbeeler
  • 669
  • 1
  • 13
  • 25
  • how are you implementing the hover? Can you make a short sample html? – Dan Pichelman Jul 27 '12 at 14:44
  • The image sources are correct, so nevermind the part about the , that's just some CI (PHP) code. Here's the pastebin: http://pastebin.com/9bmt3Zsu – willbeeler Jul 27 '12 at 14:52
  • The most obvious thing would be to check that img.png and img_hover.png are definitely both the same width and height. – Billy Moat Jul 27 '12 at 15:05
  • They are: 386 x 484, each one. – willbeeler Jul 27 '12 at 15:08
  • Post an example on jsFiddle.net. – j08691 Jul 30 '12 at 03:27
  • http://jsfiddle.net/KGNc5/ --I'll update this later with real images, etc. – willbeeler Jul 30 '12 at 03:33
  • I don't see a problem: http://jsfiddle.net/SO_AMK/cgMxe/ – A.M.K Jul 30 '12 at 04:02
  • Nevermind, change the colors and you see it. – A.M.K Jul 30 '12 at 04:04
  • I changed the colors in the link and I didn't see any shifting when using Chrome. I did use the same text so uppercase 'I' instead of lowercase. The only change was the color. Can anyone else reproduce this issue in chrome? I did see shifting when the text case changed, but I attribute that to the image generation. I think the text centering is slightly different when using a lowercase 'i'. – Michael D Johnson Jul 30 '12 at 04:21
  • I'm guess its a problem with this line: **"It even happens when I drag an element with Jquery's drag and drop functionality."** Chrome has a feature where anything dragged shows as a transparent clip of it. – James Khoury Jul 30 '12 at 04:34
  • try the Css answer for this question: http://stackoverflow.com/a/11553026/684890 – James Khoury Jul 30 '12 at 04:39

5 Answers5

6

I think (as previous answers mention) that this is a cache/loading issue. The simple fix is to set a background-image via CSS on it so that it preloads the image:

Demo: http://jsfiddle.net/SO_AMK/cgMxe/4/

HTML (CSS Declared inline so that you can still use PHP easily):

<li id="img-home">
    <img id="img-home-src" src="http://dummyimage.com/386x484/000/0011ff&text=Test+Image" style="background-image: url(<?php echo base_url();?>files/assets/images/homepage/img_hover.png);" alt="" />
</li>​

CSS: Same

jQuery: Same

A.M.K
  • 16,727
  • 4
  • 32
  • 64
  • Thanks for your answer. I apologize, but I didn't know what the problem was until a little while ago. See my answer for details. – willbeeler Aug 04 '12 at 23:23
6

Do you really need to use jQuery to toggle images on hover?
It is rather heavy solution, which also create problems with cache, because new image starts to download only on hover action.

You can use pure css to achieve your goal (improve user experience) with sprite technique, which decrease time of downloading additional images to zero, because each image for each state (hover, active) of combined in one small image, which loaded instantly:

Demo on dabblet

#img {
    width: 300px;
    height: 150px;
    background: url('//placekitten.com/g/300') no-repeat; 
}

#img:hover { background-position: 0 100%; }

Read more about sprites:

Vladimir Starkov
  • 19,264
  • 8
  • 60
  • 114
  • +1 for the pure css way. There are other 'css only' ways also but this one (IMHO) works the best. – James Khoury Jul 30 '12 at 23:30
  • That is simple solution for couple of images. But i think he want's to create some images through php dynamically (as showed in example) and because of that he need 'src' tag. – Miljan Puzović Aug 01 '12 at 01:19
  • +1 sprites are the way to go. And you should never use js for something you can achieve with css only, there are still users out there that have their js disabled... – Pevara Aug 04 '12 at 21:49
  • Thank you for this answer. I was able to fix the problem via CSS, so I completely agree with the above comments as well. My problem was a result of some issue in CSS and Chrome. However, I didn't know exactly what was causing the problem, but it's fixed now. Check out my answer below. – willbeeler Aug 04 '12 at 23:55
4

img elements are natively inline-block, so that declaration isn't needed. If you're not using a CSS reset, check out Eric Meyer's reset as a reference. It will help nullify rendering differences between browsers.

There are legitimate reasons to load images inline rather than as a CSS background - so have at it.

**HTML**
<img src="img1" class="over">
<img src="img2" class="out">

**CSS**
@import url (reset.css);

img.over, img.out { position:absolute; z-index:2; }
img.out { z-index:1; }

**jQuery**
$('.over').mouseover(
    function() {
        $(this).next('img').css('z-index',3);
    }
);
$('.out').mouseout(
    function() {
        $(this).css('z-index',2);
        $(this).prev('img').css('z-index',3);
    }
);

I'm using mouseover/out instead of hover because once the top image is hidden or pushed the bottom of the stack, the browser interprets it as hover:out (so it becomes a blinking element).

HTH

Community
  • 1
  • 1
Dawson
  • 7,567
  • 1
  • 26
  • 25
  • I appreciate your answer. It was close to fixing the problem. See my answer though. I finally found the problem and it wasn't the code I gave everyone. My apologizes. – willbeeler Aug 04 '12 at 23:22
2

That code seems legit to me. I can't put this in comments above, so it's just opinion, not answer. Maybe that happens because first image is loaded in same time when page is loaded, and secon is loaded on demand. Between hover function and full downloading image from server, background is shifted for a second. Try to call that second image somewhere on page, right on top, and give to her display:hidden or something like that. Point is to load image that will be saved in cash and used when hover function is called.

Miljan Puzović
  • 5,840
  • 1
  • 24
  • 30
1

Unfortunately, I didn't explore the issue enough to pin point precisely what the cause was, but I found the problem. I didn't mention some other parts of the code to you all because I didn't know it was the reason for the problem. So, apologizes are necessary on this one.

The culprit was the background image of the page. Here's what the CSS read:

.background {
    background:url(../images/background/5.jpg) no-repeat;
    background-size:100% auto;
    -moz-background-size: 100% auto;         /* Gecko 1.9.2 (Firefox 3.6) */
    -o-background-size: 100% auto;           /* Opera 9.5 */
    -webkit-background-size: 100% auto;      /* Safari 3.0 */
    margin:0;
    padding:0;
    opacity: 1;
    height: 100%;
    position:absolute;
    width:100% !important;
    z-index:-2000;
    left:0;
    top:0;
}

All of that code works fine in Firefox, and the other browsers, but not in Chrome. The reason there is problems in chrome is the background-size:100% auto; line. To get around this, I did this:

HTML: <img src="5.jpg" class="bg" />

CSS:

.bg {
    width:100%;
    margin:0;padding:0;border:0;
    height: auto;
    min-width:960px;
    z-index:-2000;
    position:absolute;
}

Voila. No distortion issue, in either drag and drop, and hover events.

A.M.K
  • 16,727
  • 4
  • 32
  • 64
willbeeler
  • 669
  • 1
  • 13
  • 25