Javascript not toggling element display

So, I am trying to get some slideshows on my site. I have done almost everything except for the fact that when I click on the image, the modal preview is shown, but the clicked image does not appear. I have to use the slideshow buttons to go through them and make the first one. **

If you don't want to go through all this code, you can see what's going on: http://beta.eduardstefan.com

** I'm not trying to promote myself, I just think I have a hard time understanding without an example

So my question is, how can I take the first snapshot when I click one, or why it doesn't happen, which is bad with my code?

My html:

<div class="portfolio-slideshow">
    <a class="prev" onclick="slide(0,-1)">❮</a>
    <div class="slide"> <img src="img/dailyui/008.png" class="slideimg_0" id="0" data-no="0"> </div>
    <div class="slide"> <img src="img/dailyui/007.jpg" class="slideimg_0" id="1" data-no="0"> </div>
    <div class="slide"> <img src="img/dailyui/006.jpg" class="slideimg_0" id="2" data-no="0"> </div>
    <div class="slide"> <img src="img/dailyui/003.jpg" class="slideimg_0" id="3" data-no="0"> </div>
    <a class="next" onclick="slide(0,1)">❯</a>
</div>

<div class="portfolio-slideshow">
    <a class="prev" onclick="slide(2,-1)">❮</a>
    <div class="slide"> <img src="img/dailyui/008.png" class="slideimg_2" id="0" data-no="2"> </div>
    <div class="slide"> <img src="img/dailyui/007.jpg" class="slideimg_2" id="1" data-no="2"> </div>
    <div class="slide"> <img src="img/dailyui/006.jpg" class="slideimg_2" id="2" data-no="2"> </div>
    <div class="slide"> <img src="img/dailyui/003.jpg" class="slideimg_2" id="3" data-no="2"> </div>
    <a class="next" onclick="slide(2,1)">❯</a>
</div>

<div class="imgpreview">
    <i class="fa fa-close" onclick="closepreview()"></i>
    <div class="slidepreview no_0">
        <a class="prev" onclick="slide(1,-1)">❮</a>
        <div class="slidep"> <img src="img/dailyui/008.png" class="slideimg_1" id="0"> </div>
        <div class="slidep"> <img src="img/dailyui/007.jpg" class="slideimg_1" id="1"> </div>
        <div class="slidep"> <img src="img/dailyui/006.jpg" class="slideimg_1" id="2"> </div>
        <div class="slidep"> <img src="img/dailyui/003.jpg" class="slideimg_1" id="3"> </div>
        <a class="next" onclick="slide(1,1)">❯</a>
    </div>
    <div class="slidepreview no_2">
        <a class="prev" onclick="slide(3,-1)">❮</a>
        <div class="slidep"> <img src="img/dailyui/008.png" class="slideimg_3" id="0"> </div>
        <div class="slidep"> <img src="img/dailyui/007.jpg" class="slideimg_3" id="1"> </div>
        <div class="slidep"> <img src="img/dailyui/006.jpg" class="slideimg_3" id="2"> </div>
        <div class="slidep"> <img src="img/dailyui/003.jpg" class="slideimg_3" id="3"> </div>
        <a class="next" onclick="slide(3,1)">❯</a>
    </div>
</div>

      

SCSS:

.portfolio-slideshow{
    width: 30%;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.slide{
    padding:0 35px;
    display: block;

    img{
        display: none;
        max-height:40vh;
        max-width:100%;
    }
}
.prev,.next{
    display: flex;
    align-items: center;
    justify-content: center;
}

.imgpreview{
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0,0,0,0.9);
    justify-content: center;
    align-items: center;
    padding: 50px;
}

.slidepreview{
    display: none;
    align-items: center;
    justify-content: center;

.slidep{
    display: block;

    img{
        display: none;
        max-height: 85vh;
        max-width: 85vw;
    }
}

      

And Javascript:

var slideIndex = [0,0,0,0,0,0,0,0]
function showSlides(){
    var aux = slideIndex.length;
    var i;
    for(i=0; i<aux; i+=2) { slide(i,0); }
}

function slide(n,m){
    var i;
    var aux = document.getElementsByClassName("slideimg_" + n);
    var aux2 = aux.length - 1;
    if (slideIndex[n] == 3 && m == 1) slideIndex[n] = -1;
    if (slideIndex[n] == 0 && m == -1) slideIndex[n] = 4;
    slideIndex[n] += m;
    if (slideIndex[n] < 0) slideIndex = aux2;
    else if (slideIndex[n] > aux2) slideIndex = 0;
    for(i=0; i<aux.length; i++){
        aux[i].style.display = "none";
        aux[i].parentElement.style.display = 'none';
    }
    aux[slideIndex[n]].style.display = "block";
    aux[slideIndex[n]].parentElement.style.display = 'block';
}

function closepreview(){
    $('.imgpreview').css("display" , "none");
    $('.slidepreview').css("display" , "none")
}

$(document).ready(function() {
    $(".slide img").click(function(){
        var id = $(this).attr('id');
        var no = $(this).attr('data-no');
        var no2 = no + 1;

        $(".imgpreview").css("display" , "flex");
        $(".no_" + no).css("display" , "flex");
        $("img#" + id + ".slideimg_" + no2).css("display" , "block");
        slideIndex[no2] = id;
    });
}).resize();

      

+3


source to share


2 answers


Identifiers must be unique.

Instead of built-in event handlers, I would suggest to bind them to js-code (separate html from js-code).

I removed all ids and I changed everything in jQuery using:



I used the class: active. This class is used to denote the currently active item. Moreover, every time I need to move further or forward, I move this class to the appropriate element. This class determines which img should be shown.

Excerpt:

$(document).ready(function() {
    //
    // Toggle visibility
    //
    $('.slide:not(.active), .slide:not(.active) img').toggle(false);
    $('.slide.active, .slide.active img').toggle(true);

    //
    // closing preview....
    //
    $('.imgpreview .fa.fa-close').on('click', function(e) {
        $('.imgpreview, .slidepreview').css("display" , "none");
        //
        // ...remove active class and toggle visibility
        //
        $('.imgpreview, .slidepreview').find('.active, .active img').toggleClass(false).removeClass('active');
    });

    //
    // on prev....
    //
    $(".prev").on('click', function(e) {
        //
        // get the active element and so the previous one
        //
        var active = $(this).nextAll('.slide.active');
        var prev = active.prev('.slide');
        if (prev.length == 0) {
            //
            // if at the beginning take the last one
            //
            prev = $(this).nextAll('.slide').last();
        }
        //
        // move active class and toggle visibility
        //
        active.removeClass('active');
        prev.addClass('active').find('img').andSelf().toggle(true);
        $('.slide:not(.active), .slide:not(.active) img').toggle(false);
    });

    $(".next").on('click', function(e) {
        //
        // get the active element and so the next one
        //
        var active = $(this).prevAll('.slide.active');
        var next = active.next('.slide');
        if (next.length == 0) {
            //
            // if at the end take the first one
            //
            next = $(this).prevAll('.slide').last();
        }
        //
        // move active class and toggle visibility
        //
        active.removeClass('active');
        next.addClass('active').find('img').andSelf().toggle(true);
        $('.slide:not(.active), .slide:not(.active) img').toggle(false);
    });

    $(".slide img").on('click', function(e) {
        //
        // take the index of curr element in the parent element
        //
        var idx = $(this).closest('div.slide').index();
        var no = $(this).data('no');

        $('.imgpreview, .no_' + no).css('display' , 'flex');
        $('.no_' + no).children().eq(idx).addClass('active');
        $('.no_' + no).find('.slide:not(.active), .slide:not(.active) img').toggle(false);
        $('.no_' + no).find('.slide.active, .slide.active img').toggle(true);

    });
});
      

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="http://beta.eduardstefan.com/css/style.css">
<link rel="stylesheet" href="http://beta.eduardstefan.com/css/font-awesome.css">


<div class="portfolio-slideshow">
    <a class="prev"></a>
    <div class="slide active"> <img src="http://beta.eduardstefan.com/img/dailyui/008.png" class="slideimg_0" data-no="0"></div>
    <div class="slide"> <img src="http://beta.eduardstefan.com/img/dailyui/007.jpg" class="slideimg_0" data-no="0"></div>
    <div class="slide"> <img src="http://beta.eduardstefan.com/img/dailyui/006.jpg" class="slideimg_0" data-no="0"></div>
    <div class="slide"> <img src="http://beta.eduardstefan.com/img/dailyui/003.jpg" class="slideimg_0" data-no="0"></div>
    <a class="next"></a>
</div>

<div class="portfolio-slideshow">
    <a class="prev"></a>
    <div class="slide active"> <img src="http://beta.eduardstefan.com/img/dailyui/008.png" class="slideimg_2" data-no="2"></div>
    <div class="slide"> <img src="http://beta.eduardstefan.com/img/dailyui/007.jpg" class="slideimg_2" data-no="2"></div>
    <div class="slide"> <img src="http://beta.eduardstefan.com/img/dailyui/006.jpg" class="slideimg_2" data-no="2"> </div>
    <div class="slide"> <img src="http://beta.eduardstefan.com/img/dailyui/003.jpg" class="slideimg_2" data-no="2"></div>
    <a class="next"></a>
</div>

<div class="imgpreview">
    <i class="fa fa-close"></i>
    <div class="slidepreview no_0">
        <a class="prev" onclick="slide(1,-1)"></a>
        <div class="slide"> <img src="http://beta.eduardstefan.com/img/dailyui/008.png" class="slideimg_1"></div>
        <div class="slide"> <img src="http://beta.eduardstefan.com/img/dailyui/007.jpg" class="slideimg_1"></div>
        <div class="slide"> <img src="http://beta.eduardstefan.com/img/dailyui/006.jpg" class="slideimg_1"></div>
        <div class="slide"> <img src="http://beta.eduardstefan.com/img/dailyui/003.jpg" class="slideimg_1"></div>
        <a class="next"></a>
    </div>
    <div class="slidepreview no_2">
        <a class="prev"></a>
        <div class="slide"> <img src="http://beta.eduardstefan.com/img/dailyui/008.png" class="slideimg_3"></div>
        <div class="slide"> <img src="http://beta.eduardstefan.com/img/dailyui/007.jpg" class="slideimg_3"></div>
        <div class="slide"> <img src="http://beta.eduardstefan.com/img/dailyui/006.jpg" class="slideimg_3"></div>
        <div class="slide"> <img src="http://beta.eduardstefan.com/img/dailyui/003.jpg" class="slideimg_3"></div>
        <a class="next"></a>
    </div>
</div>
      

Run codeHide result


+3


source


At least one problem here:

var no = $(this).attr('data-no');
var no2 = no + 1;

      

no

is a string, so if it no

is "0" it no + 1

is equal to "01", so you can do this:



var no = $(this).attr('data-no');
var no2 = Number(no) + 1;

      

https://codepen.io/anon/pen/MmwdZb?editors=1111

Also, why reinvent the wheel? There are many good slideshow plugins out there.

+1


source







All Articles