简体   繁体   English

一旦不再可点击,如何使下一个按钮消失?

[英]How to make a next button disappear once it's no longer clickable?

I'm trying to design a simple website with content in the center and prev and next buttons.我正在尝试设计一个简单的网站,其内容位于中心以及上一个和下一个按钮。

I would like the next button to disappear or become un-clickable once there are no more pages left to click.一旦没有更多页面可供点击,我希望下一个按钮消失或变得不可点击。

Here's what I have so far:这是我到目前为止所拥有的:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>DIV Test</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

  <div class="case active" id="content">Lorem ipsum dolor sit amet consectetur adipisicing elit. Aliquam dolores optio necessitatibus.</div>
  <div class="case" id="content">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Fugit dolore optio, fuga quae accusantium ratione possimus odit vel exercitationem porro temporibus quos repudiandae ut aperiam aliquid? Facilis enim, quas iure officia impedit labore, fugiat doloribus obcaecati, quis assumenda deleniti?</div>
  <div class="case" id="content">Lorem ipsum dolor sit amet consectetur adipisicing elit. Expedita autem consequatur perspiciatis quasi voluptatibus temporibus architecto et, qui nihil iure!</div>
  <div class="case" id="content">Lorem ipsum dolor sit amet consectetur adipisicing elit. Quas commodi mollitia ipsam?</div>

<button id="prev">&#8249;</button>
<button id="next">&#8250;</button>


</body>
<script>
$(function() {
    var nextBtn = $("#next"),
        prevBtn = $("#prev");

    nextBtn.on('click', function(e) {
        var active = $(".active");
        var next = active.next('.case');
        if (next.length) {
            active.removeClass('active');
            next.addClass('active');
        }
    });
    prevBtn.on('click', function(e) {
        var active = $(".active");
        var prev = active.prev('.case');
        if (prev.length) {
            active.removeClass('active');
            prev.addClass('active');
        }
    });
});
</script>
</html>

And the CSS style:和 CSS 样式:

.active {
    display: block !important;
  }
  .case {
      display: none;
  }
  button {
    border: none;
    background-color: white;
    color: black;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 3.2em;
    padding: 0;
    outline: 0;
  }
button:focus {
    border: 0;
    outline: 0;
}
button:active {
    transform: scale(1.2);
    background-color: white;
    border: 0;
    outline: 0;
}
  #prev {
    position: absolute;
    left: 5vw;
    bottom: 50vh;
  }
  #next {
    position: absolute;
    right: 5vw;
    bottom: 50vh;
    flex-wrap: wrap;
    justify-content: space-evenly;
      }
  button:hover{
      border: none;
      text-decoration: none;
      color: blue;
  }
  a {
    text-decoration: none;
  }

  a:hover {
    color: black;
  }

  #content {
    text-align: center;
    align-items: center;
  vertical-align: middle;
  border-color: #000000;
  border-width: 0.51px;
  border-style: solid;
  margin-top: calc(100vh - 75vh);
  margin-bottom: 25vh;
  margin-left: 15%;
  margin-right: 15%;
  border-radius: 1vw;
  padding: 1em 1em 1em 1em;
  position: relative;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  }

I want it to be aligned in the center and have switching elements.我希望它在中心对齐并具有切换元素。 Maybe i can add some transitions as well.也许我也可以添加一些过渡。

You can disable button in else block:您可以在else块中禁用按钮:

 .case{display:none;} .active{display: block;}
 <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>DIV Test</title> <link rel="stylesheet" href="style.css"> </head> <body> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="case active" id="content">Lorem ipsum dolor sit amet consectetur adipisicing elit. Aliquam dolores optio necessitatibus.</div> <div class="case" id="content">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Fugit dolore optio, fuga quae accusantium ratione possimus odit vel exercitationem porro temporibus quos repudiandae ut aperiam aliquid? Facilis enim, quas iure officia impedit labore, fugiat doloribus obcaecati, quis assumenda deleniti?</div> <div class="case" id="content">Lorem ipsum dolor sit amet consectetur adipisicing elit. Expedita autem consequatur perspiciatis quasi voluptatibus temporibus architecto et, qui nihil iure!</div> <div class="case" id="content">Lorem ipsum dolor sit amet consectetur adipisicing elit. Quas commodi mollitia ipsam?</div> <button id="prev">&#8249;</button> <button id="next">&#8250;</button> </body> <script> $(function() { var nextBtn = $("#next"), prevBtn = $("#prev"); nextBtn.on('click', function(e) { var active = $(".active"); prevBtn.prop("disabled",false); var next = active.next('.case'); if (next.length) { active.removeClass('active'); next.addClass('active'); }else{$(this).attr("disabled","true");} }); prevBtn.on('click', function(e) { nextBtn.prop("disabled",false); var active = $(".active"); var prev = active.prev('.case'); if (prev.length) { active.removeClass('active'); prev.addClass('active'); }else{$(this).attr("disabled","true");} }); }); </script> </html>

You can first store total_pages count in total_pages variable and also declare a current_page variable and then try with below code:您可以首先将total_pages计数存储在total_pages变量中,并声明一个current_page变量,然后尝试使用以下代码:

if(current_page == total_pages)
{
   $('#nextbtn').attr('disabled',true);
}
else
{
  $('#nextbtn').attr('disabled',false);
}

Maybe what you need is this, go further yourself to make the features complete, just more logic to the script :也许你需要的是这个,自己走得更远,使功能完整,只是脚本的更多逻辑

nextBtn.on('click', function(e) {
    var active = $(".active");
    var next = active.next('.case');
    if (next.length) {
        active.removeClass('active');
        next.addClass('active');
    }

    // add this: check if current page is the last page (no pages left)
    if (!next.next('.case').length) {
      nextBtn.attr("disabled", true);
    }
});

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

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