简体   繁体   English

如何在弹出窗口上制作带有下一个和上一个按钮导航的模态滑块

[英]How to make a modal slider with next and prev buttons navigation on popup

Hello so I've been trying to create the next and previous buttons to move the picture when the modal opens up to the next one, I take the pictures like this from WordPress您好,所以我一直在尝试创建下一个和上一个按钮以在模式打开到下一个时移动图片,我从 WordPress 中获取这样的图片

<?php
                        if (!empty($data['images'])):
                            foreach ($data['images'] as $img) : ?>
                                <?php if (!empty($img['image'])) : ?>
                                    <div class="slide parentSlider">
                                        <div class="slider-inner pop parentSlider-cell">
                                            <?php echo wp_get_attachment_image($img['image'], 'carousel-image', '', ['class' => 'img-responsive', 'data-track-content' => '']); ?>
                                        </div>
                                    </div>
                                <?php endif; ?>
                            <?php endforeach;
                        endif;
                        ?>

This takes all the pictures I got from WordPress and puts them on a column one after another then I added this to create the modal and show the picture when clicked on a popup with a 100% width这会获取我从 WordPress 获得的所有图片并将它们一个接一个地放在一列然后我添加它以创建模式并在单击具有 100% 宽度的弹出窗口时显示图片

<div class="modal fade bd-example-modal-lg" id="imagemodal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-body">
                            <div id="demo" class="carousel slide" data-ride="carousel">

                            <div class="carousel-inner">
                                <div class="carousel-item active">
<!--                                    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>-->
                                    <img src="" class="imagepreview" id="modal_image" style="width: 100%;" >
                                </div>
                            </div>
                                <a class="carousel-control-prev" href="#demo" data-slide="prev">
                                    <span class="carousel-control-prev-icon">&#10094;</span>
                                </a>
                                <a class="carousel-control-next" href="#demo" data-slide="next">
                                    <span class="carousel-control-next-icon">&#10095;</span>
                                </a>
                        </div>
                    </div>
                </div>
                </div>
                </div>

Here I'm trying to call the image from the PHP to here through the modal and jQuery在这里我试图通过模态和jQuery将图像从PHP调用到这里

$('.pop').on('click', function() {
                //get index for div
                var idx = $(this).parents('div').index();
                console.log(idx)
                var id = parseInt(idx);
                $('.imagepreview').attr('src', $(this).find('img').attr('src'));
                $('#imagemodal').modal('show');
                $(".carousel-inner").carousel(id); // slide carousel to selected
    });

And it shows the picture selected in the modal which is great but it doesn't work to move the picture next or previous one, I tried adding lots of other code I got online but nothing was working here ill attach the codes I tried and didn't work maybe I only implemented them wrong and someone here might know how to do it better它显示了在模态中选择的图片,这很棒,但无法将图片移动到下一张或上一张,我尝试添加很多我上网的其他代码,但这里没有任何工作,附上我尝试过但没有的代码'不起作用,也许我只是错误地实施了它们,这里的人可能知道如何做得更好

// var modalImg = $(".imagepreview");
    // $('.pop').on('click', function() {
    //     $('.imagepreview').attr('src', $(this).find('img').attr('src'));
    //     $('#imagemodal').modal('show');
    //
    // $('.next').on('click',function() {
    //     var curr = $(modalImg).attr('src',$(this).find('img').attr('src'));
    //     var next = $('img[src="' + curr +'"]').parent('div').next().find('img').attr('src');
    //     modalImg.attr('src', next);
    // });
    // $('.prev').on('click',function() {
    //     var curr = $('modalImg').attr('src',$(this).find('img').attr('src'));
    //     var prev = $('img[src="' + curr + '"]').parent('div').prev().find('img').attr('src');
    //     modalImg.attr('src', prev);
    // });
    // });
//when img is clicked
    // $('.parentSlider-cell img').click(function() {
    //     //get index for div
    //     var idx = $(this).parents('div').index();
    //     console.log(idx)
    //     var id = parseInt(idx);
    //     $('.bd-example-modal-lg').modal('show'); // show the modal
    //     $(".carousel-inner").carousel(id); // slide carousel to selected
    // });

Just wanted a simple next and prev button to move the pics around anything simple will do thank you.只想要一个简单的下一个和上一个按钮来移动图片,任何简单的事情都会做谢谢。

EDIT: For the question in the comments编辑:对于评论中的问题

<ol class="carousel-indicators" id="indicators">
                                        <li data-target="#myImg" data-slide-to="0" id="indicator">0</li>
                                        <li data-target="#myImg" data-slide-to="1" id="indicator">1</li>
                                        <li data-target="#myImg" data-slide-to="2" id="indicator">2</li>
                                    </ol>
$(".carousel-indicators").on('click', function(){

            let dataslide = $("#indicators").find(`[data-slide='${id}']`).index(this);
            //let dataslide = $("#indicators").find("data-slide").index(this); //Used this too but same result
            
            // let dsxid = dataslide.index(); - was testing with this
            // dsid = parseInt(dsxid) - testing
            console.log(dataslide);
            if(id === dataslide){
                let next_image = $('.imgs').eq(id).find('img').attr('src');
                $('.imagepreview').attr('src', next_image);
                $(".carousel-inner").carousel(id);
                $(".carousel-indicators").find("li[data-slide-to="+id+"]").addClass("active").siblings().removeClass("active");
            }
            // console.log(id);
        });

I think I got very close with making the buttons because without the index part it returns an array that shows the button for each image I'm at that time so I wanted to turn it into an index and all it as an id so that I can compare if the id is the same on click and then if not change picture or some logic like that but the index keeps returning -1 i tried to do a .filter and other code but without the id inside the data-slide but it wasn't working我想我非常接近制作按钮,因为没有索引部分它返回一个数组,显示我当时的每个图像的按钮,所以我想把它变成一个索引并将它全部作为一个 id 以便我可以比较点击时 id 是否相同,然后如果不更改图片或类似的逻辑,但索引不断返回 -1 我尝试执行 .filter 和其他代码,但数据幻灯片中没有 id 但它是不工作

You can declare global variable which will have index of the current .pop class which is selected by user so everytime you click on next button you need to first increment this index and then get the particular div at that position and using find() get the image src and add same to your imagepreview .您可以声明全局变量,该变量将具有用户选择的当前.pop类的index ,因此每次单击下一个按钮时,您需要首先增加该索引,然后在该位置获取特定的 div 并使用find()获取image src 并将其添加到您的imagepreview

Same you can do to prev button just you need to decrement index value by 1 and then use this value to get src of image and apply same to your imagepreview div .同样,您可以对prev按钮执行相同操作,只需将 index 值1 ,然后使用此值获取图像的src并将其应用于您的imagepreview div 。

Demo Code :演示代码

 var id; $('.pop').on('click', function() { var idx = $(this).parent().index(); id = parseInt(idx); $('.imagepreview').attr('src', $(this).find('img').attr('src')); $('#imagemodal').modal('show'); $(".carousel-inner").carousel(id); // slide carousel to selected }); $('.next').on('click', function() { id++; //increment if (id < $('.imgs').length) { //get id find src var next_image = $('.imgs').eq(id).find('img').attr('src'); $('.imagepreview').attr('src', next_image); //add same $(".carousel-inner").carousel(id); } }); $('.prev').on('click', function() { id--; //decremnt if (id > -1) { //find image var next_image = $('.imgs').eq(id).find('img').attr('src'); $('.imagepreview').attr('src', next_image); //add same $(".carousel-inner").carousel(id); } });
 img { height: 50px; width: 50px; }
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script> <!--addd this--> <div class="outer"> <!--add imgs class--> <div class="slide parentSlider imgs"> <div class="slider-inner pop parentSlider-cell"> <img src="http://placehold.it/1200x600/555/000&text=One" /> </div> </div> <div class="slide parentSlider imgs"> <div class="slider-inner pop parentSlider-cell"> <img src="http://placehold.it/1200x600/555/000&text=two" /> </div> </div> <div class="slide parentSlider imgs"> <div class="slider-inner pop parentSlider-cell"> <img src="http://placehold.it/1200x600/fcf00c/000&text=Three" /> </div> </div> </div> <div class="modal fade bd-example-modal-lg" id="imagemodal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-body"> <div id="demo" class="carousel slide" data-ride="carousel"> <div class="carousel-inner"> <div class="carousel-item active"> <!-- <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>--> <img src="" class="imagepreview" id="modal_image" style="width: 100%;"> </div> </div> <a class="carousel-control-prev prev" href="#demo" data-slide="prev"> <span class="carousel-control-prev-icon">&#10094;</span> </a> <a class="carousel-control-next next" href="#demo" data-slide="next"> <span class="carousel-control-next-icon">&#10095;</span> </a> </div> </div> </div> </div> </div>

 var id; $('.pop').on('click', function() { var idx = $(this).parent().index(); id = parseInt(idx); $('.imagepreview').attr('src', $(this).find('img').attr('src')); $('#imagemodal').modal('show'); $(".carousel-inner").carousel(id); // slide carousel to selected }); $('.next').on('click', function() { id++; //increment if (id < $('.imgs').length) { //get id find src var next_image = $('.imgs').eq(id).find('img').attr('src'); $('.imagepreview').attr('src', next_image); //add same $(".carousel-inner").carousel(id); } }); $('.prev').on('click', function() { id--; //decremnt if (id > -1) { //find image var next_image = $('.imgs').eq(id).find('img').attr('src'); $('.imagepreview').attr('src', next_image); //add same $(".carousel-inner").carousel(id); } });
 img { height: 50px; width: 50px; }
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script> <!--addd this--> <div class="outer"> <!--add imgs class--> <div class="slide parentSlider imgs"> <div class="slider-inner pop parentSlider-cell"> <img src="http://placehold.it/1200x600/555/000&text=One" /> </div> </div> <div class="slide parentSlider imgs"> <div class="slider-inner pop parentSlider-cell"> <img src="http://placehold.it/1200x600/555/000&text=two" /> </div> </div> <div class="slide parentSlider imgs"> <div class="slider-inner pop parentSlider-cell"> <img src="http://placehold.it/1200x600/fcf00c/000&text=Three" /> </div> </div> </div> <div class="modal fade bd-example-modal-lg" id="imagemodal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-body"> <div id="demo" class="carousel slide" data-ride="carousel"> <div class="carousel-inner"> <div class="carousel-item active"> <!-- <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>--> <img src="" class="imagepreview" id="modal_image" style="width: 100%;"> </div> </div> <a class="carousel-control-prev prev" href="#demo" data-slide="prev"> <span class="carousel-control-prev-icon">&#10094;</span> </a> <a class="carousel-control-next next" href="#demo" data-slide="next"> <span class="carousel-control-next-icon">&#10095;</span> </a> </div> </div> </div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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