Change the background-image of the div according to the banner image

Asked

Viewed 2,080 times

0

I need to create an effect on my slideshow that for each image displayed in the slideshow the background of div class="banner-backg" change the background-image.

For the slider I’m using the http://bxslider.com/options

: HTML by Slider

<div class="banner-backg">
    <div class="banner-center">
        <ul class="bxslider">
            <li><img src="images/banner_1.png" /></li>
            <li><img src="images/banner_2.png" /></li>
            <li><img src="images/banner_3.png" /></li>
        </ul>
    </div>
</div>

: jQuery

$('.bxslider').bxSlider({
    adaptiveHeight: true,
    mode: 'fade',
    auto:true,
    easing:'easeInOutCubic',
    useCSS:false,
    speed: 1000
});

An alternative I thought was to manually insert the background-image for each image. Example:

            <li><img src="images/banner_1.png" setimg="images/background_1jpg" /></li>
            <li><img src="images/banner_2.png" setimg="images/background_2jpg" /></li>

In that, I would run a jQuery that took the value setimg and applied as background in div class="banner-backg". But I don’t have much knowledge of how I can create this variable within the img.

  • You can do this with Callbacks functions, in his documentation you can do it.

  • I tried to use onSlideNext but it only works by changing the background color, I don’t know how I can capture the image path .

1 answer

0


In your HTML you put like this:

<div class="banner-backg">
<div class="banner-center">
    <ul class="bxslider">
        <li><img src="images/banner_1.png" /></li>
        <li><img src="images/banner_2.png" /></li>
        <li><img src="images/banner_3.png" /></li>
    </ul>
</div>

Consider that in the folder images have the files listed below and the library jquery with the version you implement .css():
background_1.jpg
background_2.jpg
background_3.jpg

And in the script you do something like this:

$('.bxslider').bxSlider({
    adaptiveHeight: true,
    mode: 'fade',
    auto:true,
    easing:'easeInOutCubic',
    useCSS:false,
    speed: 1000,
    onSlideNext: function($slideElement, oldIndex, newIndex) {
   // var tamanho = {'width':'100%', 'height':'auto'};
         $('.bxslider')
          .css({'background':'url(images/background_' + newIndex + '.jpg) center center no-repeat'})
        //  .css(tamanho)
        ;  
  }
});
  • I didn’t test it, but check it out.

  • It was worth a shot, but it didn’t work, Ivan. he managed to make it work with COLOR, in my case I just wanted to change the color by image. http://answall.com/questions/36892/alterar-background-de-acordo-com-a-imagem/36916#36916

  • Dude, it’s the same logic, newindex would be the image number: background_1.jpg, background_2.jpg, background_2.jpg, etc... what I did was actually access the element’s Parent(), which in this case is <ul>, and put a background image. Just set the image as background.

  • Very good Ivan... it did... only instead of putting the background to appear in div . bxslider I put in div . banner-backg .... Thank you so much for your help, you saved me here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.