简体   繁体   English

隐藏/禁用最后一张幻灯片上的“下一步”按钮和第一张幻灯片上的“上一页”按钮

[英]Hide/disable 'Next' button on last slide and 'Prev' button on first slide

I found a script online and used it to make lightbox and slideshow for images on my website. 我在网上找到了一个脚本,并用它来制作网站上的图片的灯箱和幻灯片。 The problem is that it doesn't have the option to disable next button on last slide and prev button on first slide. 问题在于它没有选择禁用上一张幻灯片上的下一个按钮和上一张幻灯片上的上一个按钮的选项。 So I am posting the code here. 所以我在这里发布代码。

My gallery consists of few sections which represent the albums (school projects, personal projects, etc). 我的画廊由代表专辑的几个部分组成(学校项目,个人项目等)。 I wrote the script in JS: when you click on a certain section name, it hides one section and shows another. 我用JS编写了脚本:当您单击某个节的名称时,它将隐藏一个节并显示另一个节。 When I list the images, it jumps from the last image in one section to the first image in the next section. 当我列出图像时,它会从一个部分中的最后一个图像跳到下一个部分中的第一个图像。

So not only that I need help with next and prev buttons on last and first image, but I also need the script to recognize the amount of images in one section and hide the next button on the last slide in that section (also for the prev button). 因此,不仅我需要有关最后一张和第一张图片上的下一页和上一页按钮的帮助,而且我还需要脚本来识别一个部分中的图像数量并隐藏该部分中最后一张幻灯片上的下一个按钮(也适用于上一张)按钮)。 Keep in mind that I am a complete beginner in JS. 请记住,我是JS的完整入门者。 I am making this website for my school project. 我正在为我的学校项目创建此网站。

 // Lightbox jQuery(document).ready(function($) { // global variables for script var current, size; $('.lightboxTrigger').click(function(e) { // prevent default click event e.preventDefault(); // grab href from clicked element var image_href = $(this).attr("href"); // determine the index of clicked trigger var slideNum = $('.lightboxTrigger').index(this); // find out if #lightbox exists if ($('#lightbox').length > 0) { // #lightbox exists $('#lightbox').fadeIn(300); // #lightbox does not exist - create and insert (runs 1st time only) } else { // create HTML markup for lightbox window var lightbox = '<div id="lightbox">' + '<p class="close">&times;</p>' + '<div id="slideshow">' + '<ul></ul>' + '<div class="nav">' + '<a href="#prev" class="prev slide-nav">&#10094;</a>' + '<a href="#next" class="next slide-nav">&#10095;</a>' + '</div>' + '</div>' + '</div>'; //insert lightbox HTML into page $('body').append(lightbox); // fill lightbox with .lightboxTrigger hrefs in .gallery $('.gallery').find('.lightboxTrigger').each(function() { var $href = $(this).attr('href'); $('#slideshow ul').append( '<li>' + '<img src="' + $href + '">' + '</li>' ); }); } // setting size based on number of objects in slideshow size = $('#slideshow ul > li').length; // hide all slide, then show the selected slide $('#slideshow ul > li').hide(); $('#slideshow ul > li:eq(' + slideNum + ')').show(); // set current to selected slide current = slideNum; }); //Click anywhere on the page to get rid of lightbox window $('body').on('click', '#lightbox', function() { // using .on() instead of .live(). more modern, and fixes event bubbling issues $('#lightbox').fadeOut(300); }); // navigation prev/next $('body').on('click', '.slide-nav', function(e) { // prevent default click event, and prevent event bubbling to prevent lightbox from closing e.preventDefault(); e.stopPropagation(); var $this = $(this); var dest; // looking for .prev if ($this.hasClass('prev')) { dest = current - 1; if (dest < 0) { dest = size - 1; } } else { // in absence of .prev, assume .next dest = current + 1; if (dest > size - 1) { dest = 0; } } // fadeOut curent slide, FadeIn next/prev slide $('#slideshow ul > li:eq(' + current + ')').fadeOut(750); $('#slideshow ul > li:eq(' + dest + ')').fadeIn(750); // update current slide current = dest; }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="gallery school"> <h3>School projects</h3> <a class="lightboxTrigger" href="portfolio/school/img1.jpg"><img class="thumb" src="portfolio/school/img1.jpg" alt="x"></a> <a class="lightboxTrigger" href="portfolio/school/img2.jpg"><img class="thumb" src="portfolio/school/img2.jpg" alt="x"></a> <a class="lightboxTrigger" href="portfolio/school/img3.jpg"><img class="thumb" src="portfolio/school/img3.jpg" alt="x"></a> </div> <div class="gallery personal"> <h3>Personal projects</h3> <a class="lightboxTrigger" href="portfolio/personal/img1.jpg"><img class="thumb" src="portfolio/school/img1.jpg" alt="x"></a> <a class="lightboxTrigger" href="portfolio/personal/img2.jpg"><img class="thumb" src="portfolio/school/img2.jpg" alt="x"></a> <a class="lightboxTrigger" href="portfolio/personal/img3.jpg"><img class="thumb" src="portfolio/school/img3.jpg" alt="x"></a> </div> 

To slide through the currently selected gallery, you need to get the slide number determined by the index of the trigger inside the parent gallery 要通过当前选定的库幻灯片,你需要得到幻灯片编号由触发器 父画廊 内部 编号确定

I achieved this with 我做到了

var slideNum = $(this).closest('.gallery').find('.lightboxTrigger').index(this);

What this does is finds the closest .gallery parent, and then indexes the triggers, instead of indexing all of them from the start. 它的作用是找到最接近的.gallery父对象,然后为触发器建立索引,而不是从一开始就为所有触发器建立索引。

Here's a working codepen: https://codepen.io/xhynk/pen/GQVWpm?editors=1010 这是一个有效的Codepen: https ://codepen.io/xhynk/pen/GQVWpm editors = 1010

Note: You didn't have any styles so I cobbled together some rather ugly ones real quick, also since your images were relative, I used "lorempixel" - so the actual images won't show up properly, and the slideshow will have 6 of the same image most likely, just for demonstration purposes. 注意:您没有任何样式,因此我快速地将一些相当丑陋的样式拼凑在一起,而且由于您的图像是相对的,因此我使用了“ lorempixel”-这样实际的图像将无法正确显示,幻灯片将显示6最有可能是同一图像的图像,仅用于演示目的。

The things of note in the JS are in the initial $('.lightboxTrigger').click(function(e) { JS中的注意事项在初始$('.lightboxTrigger').click(function(e) {

   if( slideNum == 0 ){
        $('.prev').hide();
    } else {
        $('.prev').show();
    }

    if( slideNum == size - 1 ){
        $('.next').hide();
    } else {
        $('.next').show();
    }

And similar code in the prev/next nav: $('body').on('click', '.slide-nav', function(e) { 上一个/下一个导航中的类似代码: $('body').on('click', '.slide-nav', function(e) {

    if( current == 0 ){
        $('.prev').hide();
    } else {
        $('.prev').show();
    }

    if( current == size - 1 ){
        $('.next').hide();
    } else {
        $('.next').show();
    }

As well as the indexing based on parent gallery mentioned above 以及上述基于父库的索引

var slideNum = $(this).closest('.gallery').find('.lightboxTrigger').index(this);

The gallery is also only filled with the images from the parent gallery instead of all of them, using: 画廊也只填充了来自父画廊的图像,而不是全部,使用:

$(this).closest('.gallery').find('.lightboxTrigger').each(function() {

And lastly this code will still fadeOut the gallery when closed as you had it, but it will also destroy it with remove after the fade is completed. 最后,此代码在关闭时仍会按您的意愿逐渐fadeOut图库,但是淡入淡出完成后,也会通过remove将其销毁。 This allows a new gallery to be indexed and used, without allowing a user to slide through them in an interconnected manner. 这允许索引和使用新的画廊,而无需用户以互连的方式在它们之间滑动。

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

相关问题 Jquery 滑块问题-禁用最后一张幻灯片上的下一个按钮和第一张幻灯片上的上一个按钮 - Jquery slider issue-disable next button on last slide and prev button on first slide 在第一张幻灯片上禁用 Prev Control 并在最后一张幻灯片上禁用 Next control - Disable Prev Control on first slide and disable Next control on last slide Slider 手动控制允许上一个/下一个按钮从最后一张幻灯片单击到第一张幻灯片 - Slider manual control to allow prev/next button to click from last slide to first slide jQuery prev / next滑块 - &gt;如何在第一张/最后一张幻灯片上禁用prev / next? - jQuery prev/next slider --> how to disable prev/next on first/last slide? 将下一个和上一个按钮添加到我的幻灯片 - Adding Next and Prev button to my slide 如何在幻灯片放映中添加上一个和下一个按钮 - How to add prev and next button in slide show 如何获取上一张和第一张幻灯片以激活下一个和上一个按钮或相应地禁用 - how to get last and first slide to make next and previous button active or disable accordingly 禁用特定幻灯片上的按钮 - Disable button on specific slide 下一张幻灯片按钮 - Next slide button 分别到达最后一张和第一张幻灯片后,如何使“下一个”按钮和“上一个”按钮不起作用? - How to make Next button and Previous button non-working after I reach last and first slide respectively?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM