vaskort
vaskort

Reputation: 2861

Change background image onclick with easing

I am implementing a site and I would like to change background images on click but with ease, like fade in fade out or whatever.

The onclick part is implemented with Jquery but I am struggling with the easing part.

I have searched the Web for this but every solution is using a div only in a small part of the page.

The problem for me is that I use these divs as a whole page, 100% width and height and I have content in front of the divs.

I thought about using sprites and animate the background position but that doesn't help because I want my page to be responsive and I have percentage on background url and sprites need you to declare fixed width (correct me if I'm wrong).

Also I must add that behind the divs there is an other div, so changing opacity solution can't help. I am implementing a site like this: http://www.samsung.com/global/microsite/galaxynote3-gear/

HTML:

<div class="Page" id="feauture3">
    <div id="feauture3_content">
        <div id="feauture3_1"><strong>Menu1</strong></div>
        <div id="feauture3_2"><strong>Menu2</strong></div>
        <div id="feauture3_3"><strong>Menu3</strong></div>
        <div id="feauture3_4"><strong>Menu4</strong></div>
    </div>
</div>

CSS:

#feauture3_1:hover {
        background-color:#f2af95;
        cursor:pointer;
}

#feauture3_2:hover {
        background-color:#f2af95;
        cursor:pointer;
}

#feauture3_3:hover {
        background-color:#f2af95;
        cursor:pointer;
}

#feauture3_4:hover {
        background-color:#f2af95;
        cursor:pointer;
}

#feauture3 {
        position: fixed;
        height: 100%;
        width: 100%;
        background: url('http://www.asphaltandrubber.com/wp-content/gallery/2013-kawasaki-ninja-z800-z800e/2013-kawasaki-ninja-z800-02.jpg') 50% 50% no-repeat;
        left:0; 
        background-size: cover;
        background-color:#e18764;
        color:red;
}

Jquery:

jQuery(document).ready(function($){
       $("#feauture3_1").click(function(){
       $("#feauture3").css('background-image','url("http://www.motorcyclespecs.co.za/Gallery%20B/Kawasaki%20Ninja%20650R%2013.jpg")');
    });

    $("#feauture3_2").click(function(){
        $("#feauture3").css('background-image','url("http://images5.fanpop.com/image/photos/31700000/HOT-BABE-KAWASAKI-Z1000-motorcycles-31778270-1920-1200.jpg")')
    });

    $("#feauture3_3").click(function(){
        $("#feauture3").css('background-image','url("http://b296d35169b22ec514a7-3f0e5c3ce41f2ca4459ddb89d451f8d9.r21.cf2.rackcdn.com/wp-content/uploads/2012/11/Kawasaki-Z1-by-Ac-Sanctuary-.jpg")')
    });

    $("#feauture3_4").click(function(){
        $("#feauture3").css('background-image','url("http://4.bp.blogspot.com/-ar4zyO_Ws4M/UekF8jk7nRI/AAAAAAAA1q4/ugQZlRGTLkk/s1600/Kawasaki-Z-1000-.jpg")')
    });

});

Fiddle:

http://jsfiddle.net/9pWhN/1/

Thanks for your time.

Edit: I finally used a simple $("#feauture3").css('background-image','url("image")') and set a background-color to the whole div that matches the images(the real project dont have motorcycles as images). This was quite acceptable and I used this solution.

Upvotes: 5

Views: 6456

Answers (6)

d.abyss
d.abyss

Reputation: 212

I believe this may be what you are looking for -

http://css3.bradshawenterprises.com/cfimg/#cfimg7

Loading all the images on top of each other before transitioning them will not create the 'flash' you are talking about.

Upvotes: 0

Karthik Naidu
Karthik Naidu

Reputation: 141

Here is the fix - test for proper loading once both the external images loaded -

  jQuery(document).ready(function($){
  var img1 = 'http://www.motorcyclespecs.co.za/Gallery%20B/Kawasaki%20Ninja%20650R%2013.jpg',
      img2 = 'http://images5.fanpop.com/image/photos/31700000/HOT-BABE-KAWASAKI-Z1000-motorcycles-31778270-1920-1200.jpg',
      target = $('#feauture3');

      $("#feauture3_1").click(function(){
          target.fadeOut(1000, function(){
              target.css('background-image', 'url('+img1+')');  
              target.fadeIn(500);
          });
      });

      $("#feauture3_2").click(function(){
          target.fadeOut(1000, function(){
              target.css('background-image', 'url('+img2+')');  
              target.fadeIn(500);
          });
      });  

});

Upvotes: 0

yeyene
yeyene

Reputation: 7380

Try this?

DEMO http://jsfiddle.net/9pWhN/6/

Add .fadeOut() before image change, and then .fadeIn() again with setTimeout().

JQUERY

$("#feauture3_1").click(function(){
   $("#feauture3").fadeOut(1000);
   setTimeout(function() {
       $("#feauture3")
           .css('background-image','url("http://www.motorcyclespecs.co.za/Gallery%20B/Kawasaki%20Ninja%20650R%2013.jpg")')
           .fadeIn(1000);     
   }, 1000);       
});

Upvotes: 0

Lokesh Suthar
Lokesh Suthar

Reputation: 3202

How about css transitions ?

#feauture3 {
    position: fixed;
    height: 100%;
    width: 100%;
    background: url('http://www.asphaltandrubber.com/wp-content/gallery/2013-kawasaki-ninja-z800-z800e/2013-kawasaki-ninja-z800-02.jpg') 50% 50% no-repeat;
    left:0;
    background-size: cover;
    background-color:#e18764;
    color:red;
    -webkit-transition:all 1.4s ease-in-out;
    -o-transition:all 1.4s ease-in-out;
    -moz-transition:all 1.4s ease-in-out;
}

Try this fiddle

Upvotes: 2

danwarfel
danwarfel

Reputation: 880

Try doing this to your features:

    $("#feauture3_2").click(function(){
    $("#feauture3").css({
        'background-image' :  'url("http://images5.fanpop.com/image/photos/31700000/HOT-BABE-KAWASAKI-Z1000-motorcycles-31778270-1920-1200.jpg")',
        'opacity': 0
    })
    .animate({
        'opacity': 1
    }, 800);
});

Just apply that to each click event.

Upvotes: 0

Farkhat Mikhalko
Farkhat Mikhalko

Reputation: 3645

We can divide this problems into several parts:

  1. How to add easing or fading effects to images answer is using CSS 3

    #feauture3 {
    position: fixed;
    height: 100%;
    width: 100%;
    background: url('http://www.asphaltandrubber.com/wp-content/gallery/2013-kawasaki-ninja-z800-z800e/2013-kawasaki-ninja-z800-02.jpg') 50% 50% no-repeat;
    left:0; 
    background-size: cover;
    background-color:#e18764;
    color:red;
    -webkit-transition: all 500ms cubic-bezier(0.250, 0.100, 0.250, 1.000); 
    -moz-transition: all 500ms cubic-bezier(0.250, 0.100, 0.250, 1.000); 
     -o-transition: all 500ms cubic-bezier(0.250, 0.100, 0.250, 1.000); 
        transition: all 500ms cubic-bezier(0.250, 0.100, 0.250, 1.000);
    }
    

  2. How detect that image loaded and add src answer is using js hack

$("<img>").attr('src', 'http://picture.de/image.png').load(function() {
     $(this).remove();
     $('body').css('background-image', 'url(http://picture.de/image.png)');
});

Play with css3 easing fading

Question about image loading link

Upvotes: 0

Related Questions