美文网首页前端
JS代码实现(翻页)页面切换效果上一页具体页下一页

JS代码实现(翻页)页面切换效果上一页具体页下一页

作者: 兰觅 | 来源:发表于2021-05-07 16:23 被阅读0次

    一.效果图

    效果图

    二.HTML页

    以下部分为源码 ,来源:https://blog.csdn.net/weixin_44189739/article/details/85342424

    根据需求后修改如下:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="renderer" content="webkit"/>
    <meta name="force-rendering" content="webkit"/>
    <title></title>
    <link type="text/css" rel="stylesheet" href="css/style.css" />
    <link rel="shortcut icon" href="images/ilogo.png" type="image/x-icon">
    
    <script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
    <script type="text/javascript" src="js/banner.js"></script>
    </head>
    <body>
    //html主要内容举例如下:
    <div class="item active">
        <ul class="piclistul active">
          <li>
            <a href="picdetail.html">
            <img src="images/2.jpg">
            <p class="ccsl">智能耳机</p>
            </a>
          </li>
          <li>
            <a href="picdetail.html">
            <img src="images/2.jpg">
            <p class="ccsl">智能耳机</p>
            </a>
          </li>
          <li>
            <a href="picdetail.html">
            <img src="images/2.jpg">
            <p class="ccsl">智能耳机</p>
            </a>
          </li>
          <li>
            <a href="picdetail.html">
            <img src="images/2.jpg">
            <p class="ccsl">智能耳机</p>
            </a>
          </li>
          <li>
            <a href="picdetail.html">
            <img src="images/2.jpg">
            <p class="ccsl">智能耳机</p>
            </a>
          </li>
          <li>
            <a href="picdetail.html">
            <img src="images/2.jpg">
            <p class="ccsl">智能耳机</p>
            </a>
          </li>
        </ul>
        </div>
        
    <div class="item"> 
        <ul class="piclistul">
          <li>
            <a href="picdetail.html">
            <img src="images/jiu.jpg">
            <p class="ccsl">威士忌</p>
            </a>
          </li>
          <li>
            <a href="picdetail.html">
            <img src="images/jiu.jpg">
            <p class="ccsl">威士忌</p>
            </a>
          </li>
          <li>
            <a href="picdetail.html">
           <img src="images/jiu.jpg">
            <p class="ccsl">威士忌</p>
            </a>
          </li>
          <li>
            <a href="picdetail.html">
            <img src="images/jiu.jpg">
            <p class="ccsl">威士忌</p>
            </a>
          </li>
          <li>
            <a href="picdetail.html">
            <img src="images/jiu.jpg">
            <p class="ccsl">威士忌</p>
            </a>
          </li>
        </ul>
      </div>
    </body>
    //引入js文件
    <script type="text/javascript" src="./js/utils.js"></script>
    </html>
    

    二.JS部分:

    utils.js文件

    function toggle(eles, active) {
        //封装函数、图片显示的部分、传入获取到的div,和被点击的序号
        for (var i = eles.length; i--;) {
            eles[i].className = "item"; //先让所有div隐藏
        }
        eles[active].className = "item active"; //再让被点击的序号对应的div 显示
    }
    
    
    //下一页
    window.onload=function(){
        var abtn = document.getElementsByClassName("btn");
        var aItem = document.getElementsByClassName("item");
        
        var nowPage = 0; //定义当前也,默认值为0
        
        for (var i = abtn.length; i--;) {
            abtn[i].tab = i;
            abtn[i].onclick = function() {
                toggle(aItem, this.tab);
                nowPage = this.tab //被点击后,保存当前页的序号
            }
        }
        
        var next = document.getElementsByClassName("next");
        next[0].onclick = function() {
            nowPage++;
            nowPage %= abtn.length;
            toggle(aItem, nowPage);
        }
        //上一页
        var prev = document.getElementsByClassName("pre");
        prev[0].onclick = function() {
            nowPage--;
            if (nowPage == -1) {
                nowPage = abtn.length - 1;
            }
            toggle(aItem, nowPage);
        }
    }
    

    相关文章

      网友评论

        本文标题:JS代码实现(翻页)页面切换效果上一页具体页下一页

        本文链接:https://www.haomeiwen.com/subject/clxedltx.html