简体   繁体   中英

slider with opacity and z-index not working properly

i am quite new to JQuery. I am working on a slider which shows the image when the thumb is clicked. Now i have given some data-roles to the thumbs and the full image so that if thumb no.3 is clicked, the full image of data-id 3 is set to opacity 1 and z-index larger. Somehow, the slider works first time when the thumbs are clicked, but when i click on second time the image is not shown which has same i as thumb. here is my code

HTML

<div id="sliderContainer">
    <!--Filters -->
    <ul id="filterList">
        <li>All</li>
        <li>Objects</li>
        <li>Fashion</li>
        <li>Nature</li>
    </ul>

    <span id="titleText">asdsd</span>
    <!--Thumbs List-->
    <ul id="thumbsList">
        <li class="thumbs" data-thumbid="1" data-title="Girl Eating Something"><img src="images/fashion/fashion1Thumb.jpg" /></li>
        <li class="thumbs" data-thumbid="2" data-title="Beautiful Face"><img src="images/fashion/fashion2Thumb.jpg" /></li>
        <li class="thumbs" data-thumbid="3" data-title="Cinderella"><img src="images/fashion/fashion3Thumb.jpg" /></li>
        <li class="thumbs" data-thumbid="4" data-title="Apple Mobile"><img src="images/objects/object1Thumb.jpg" /></li>
        <li class="thumbs" data-thumbid="5" data-title="Coke Can"><img src="images/objects/object2Thumb.jpg" /></li>
        <li class="thumbs" data-thumbid="6" data-title="Mountains"><img src="images/nature/nature1thumb.jpg" /></li>
    </ul>


    <img class="productsSliderImage" data-fullimageid="1" src="images/fashion/fashion1Full.jpg" />
    <img class="productsSliderImage" data-fullimageid="2" src="images/fashion/fashion2Full.jpg" />
    <img class="productsSliderImage" data-fullimageid="3" src="images/fashion/fashion3Full.jpg" />
    <img class="productsSliderImage" data-fullimageid="4" src="images/objects/object1Full.jpg" />
    <img class="productsSliderImage" data-fullimageid="5" src="images/objects/object2Full.jpg" />
    <img class="productsSliderImage" data-fullimageid="6" src="images/nature/nature1Full.jpg" />
    <img class="productsSliderImage" data-fullimageid="7" src="images/nature/nature1Full.jpg" />

</div>

and here is my JQUERY

$(document).ready(function () {
    $(".productsSliderImage").css('opacity', '0');
});

$(document).ready(function () {
    $('.thumbs').click(function () {
        var currentThumbId = $(this).attr("data-thumbid"); //grab thumbId of clicked thumb.
        //Changing css of the fullscreenImage which is equal to clcked thumb.
        //Animation is done using Opacity in css.
        $('.productsSliderImage[data-fullImageId="' + currentThumbId + '"]').css('z-index', '33');
        $('.productsSliderImage[data-fullImageId="' + currentThumbId + '"]').css('opacity', '1');
        var notClicked = $('.productsSliderImage[data-fullImageId="' + currentThumbId + '"]').not(this);
        notClicked.css('opacity', '1');
        notClicked.css('z-index', '1');
    });
});

And my Css

.productsSliderImage {
    position: absolute;
    z-index: 1;

    -webkit-transition: all 0.8s ease;
    -moz-transition: all 0.8s ease;
    -ms-transition: all 0.8s ease;
    -o-transition: all 0.8s ease;
    transition: all 0.8s ease;
}

Try it like,

CSS

.animate{
   zIndex:33;
   opacity:1
}

SCRIPT

$('.thumbs').click(function () {
    var thumbId = $(this).data("thumbid"); //use data function    
    // remove class for all products
    $('.productsSliderImage').removeClass('animate');
    // set current product image class to overlap
    $('.productsSliderImage[data-fullImageId="'+thumbId+'"]').addClass('animate');
});

you can always create a class with the properties you wanna add such as z index and opacity rather than writing lengthy code. you can use addClass() and removeClass() methods to do that. Read here addClass() Method Info.

Thanks.

So Basically you can do something like this .animation

{ blah blah blah } then i jquery, you can use something like this onclick()

$('#abc').addClass('animation');

and the same way you can remove class. thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM