8358

Preventing image flicker on hover when replacing it with another image (CSS)

When I hover my cursor on the logo the first time after the page loads, it starts to blink rapidly for about a second. I've thought about using sprites, but I don't want to set the logo as the background image - I already have one. Here's my CSS code:

<!DOCTYPE HTML> <head> <style> html { background-image:url('backgroundimage.jpg'); -webkit-background-size: 100%; -moz-background-size: 100%; -o-background-size: 100%; background-size: 100%; -webkit-filter: brightness(0.6); -moz-filter: brightness(0.6); -o-filter: brightness(0.6); -ms-filter: brightness(0.6); } #logo { position: fixed; top: 50%; left: 50%; margin-top: -250px; margin-left: -250px; } #logo:hover { content: url('logobeforehover.png'); } </style> </head> <body> <div id="logo"> <img src="logoafterhover.png"/> </div> </body> </html>

Answer1:

There will always be some flicker, since the user's browser will have to load the image from the server. TCP & HTTP have some amount of overhead, and the fastest you can reasonably expect (even for very small images) is about 100-200ms. Which is often noticeable. In reality, the time is often higher.

Possible solutions:

    <li>Place the image in the page somewhere, but don't display it. Setting display: none is often used, but that doesn't seem reliable anymore; alternatively, you can add an image with a width & height of 1px and z-index of -1. IMHO this is ugly. Note that this also increases initial page load.</li> <li>

    Don't use CSS, but Javascript to switch the image. This way you can pre-load the image, and change it when it's loaded & ready, for example:

    var img = new Image(); img.onload = function() { $('#logo').attr('src', $(this).attr('src')); }; img.src = 'example.com/image' </li> <li>

    Use a CSS sprite, you already mentioned you don't want to do that, but I'll list it for completion's sake. IMHO, it is the best solution.

    </li> </ul>

    Answer2:

    Its simple... Add same Width and Height in both #logo and #logo:hover like this

    #logo { position: fixed; top: 50%; left: 50%; margin-top: -250px; margin-left: -250px; width:XXpx; height:XXpx; } #logo:hover { content: url('logobeforehover.png'); width:XXpx; height:XXpx; }

Recommend

  • Firefox includes border in height of whilst chrome does not
  • Change the width of the JQM panels
  • Safari PHP form submission -file upload hangs
  • css calendar - td background diagonal split - two colors
  • Cursor in wrong place in contenteditable
  • Android - Calling Java from Webview
  • Reading space separated values file in c++ error
  • Flex items with same property values are rendering in different sizes
  • cell spacing in div table
  • Cut the background to expose the layer below
  • Floated image with variable width and heading with background image
  • RxJava debounce by arbitrary value
  • Adjust width of select element according to selected option's width
  • how to display data from 1st point on words on y axis for line chart in d3.js
  • Blackberry - Custom EditField Cursor
  • HTTP/2 streams vs HTTP/1.1 connections
  • Time complexity of a program which involves multiple variables
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • Custom validator control occupying space even though display set to dynamic
  • Listbox within Listbox and scrolling trouble in Windows Phone 7 Silverlight
  • Atlas images wrong size on iPad iOS 9
  • $wpdb not working in file of WordPress plugin
  • Disable Enter in editText android
  • Change multiple background-images with jQuery
  • Algorithm for a smudge tool?
  • Body moving without any force applied? (Box2d)
  • CSS Linear-gradient formatting issue accross different browsers
  • Android screen density dpi vs ppi
  • RectangularRangeIndicator format like triangular using dojo
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Can Jackson SerializationFeature be overridden per field or class?
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • Menu Color Fade on Hover with Jquery
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • How to check if every primary key value is being referenced as foreign key in another table
  • Change an a tag attribute in JavaScript based on screen width
  • Android Studio and gradle
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • python regex in pyparsing
  • Android Google Maps API OnLocationChanged only called once