0

Right now im using css to create an onclick image change. The problem is on my website when i use it, there is a pause for the image to load. I was wondering if there is a javascript or jquery alternative that doesnt require for the image to be loaded when i click it, making it white in the back for a few seconds after i click it until it loads.

Right now this is what im using.Example Here

<input type="submit" class="create" style="font: 24px Arial, Helvetica, sans-serif; width: 681px;" value="CREATE ">
​
.create {
    margin: 0 auto;
    height: 62px;
    width: 681px;
    color:#FFF;
    background-image:url(http://i.imgur.com/tWGcy.jpg);
    outline: 0;
    border: 0;
    margin-top: 7px;
    font-family: Arial, Helvetica, sans-serif;
}
.create:active {
    margin: 0 auto;
    height: 62px;
    width: 681px;
    color:#FFF;
    background-image:url(http://i.imgur.com/r9d3C.jpg);
    outline: 0;
    border: 0;
    margin-top: 7px;
    font-size: 40px;
    font-family: Arial, Helvetica, sans-serif;
}​
0

3 Answers 3

2

There no need for any JS. You can use css sprites for this & it's a better solution.

Read this article for more http://css-tricks.com/css-sprites/

Sign up to request clarification or add additional context in comments.

1 Comment

Interesting i have heard about this.
1

what you need to do is splice those image into one big image, then just change the left or top like this: DEMO

CSS:

button {
   background:url(http://anton.shevchuk.name/wp-content/uploads/2009/07/button-sprite-result.png); 
   border:0;
   padding:0;   
   width:310px;
   height:80px;
}

button:hover {
   background-position:0 -70px;   
}

button:active {
   background-position:0 -140px;   
}

HTML:

<button></button>

​ ​it's a technique called spriting, hope this helps -ck

4 Comments

Yeah i am looking into it now actually.
yeah sorry, already was 80% done making demo when i saw someone beat me to the punch...
Im using a sprite generating site to make them for me :) But the code will help a million.
@soniccool glad to hear it, as a general rule of thumb, when doing GUI stuff, do as much of it in CSS as possible :-)
1

There are a number of ways to solve your issue,

Your situation argues for a solution using css image sprite.

Sprite up your image with both hover and normal background in same image, then change just background position on hover, this way no new image needs to be changed.

css-tricks tutorial You dont need all that stuff for your simple sprite but its worth reading

Another solution will be to preload images with javascript.

var img = new Image();
img.src = "http://i.imgur.com/r9d3C.jpg";

Using image sprites looks the cleaner solution, but not applicable to all conditions though.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.