html轮播图

13,082次阅读
没有评论

共计 3509 个字符,预计需要花费 9 分钟才能阅读完成。

  • 作者简介 :一名后端开发人员,每天分享后端开发以及人工智能相关技术,行业前沿信息,面试宝典。
  • 座右铭 :未来是不可确定的,慢慢来是最快的。
  • 个人主页 :极客李华 -CSDN 博客
  • 合作方式 :私聊 +
  • 这个专栏内容 :BAT 等大厂常见后端 java 开发面试题详细讲解,更新数目 100 道常见大厂 java 后端开发面试题。
  • 我的 CSDN 社区 :https://bbs.csdn.net/forums/99eb3042821a4432868bb5bfc4d513a8
  • 微信公众号,抖音,b 站等平台统一叫做 :极客李华,加入微信公众号领取各种编程资料,加入抖音,b 站学习面试技巧

简介 :本博客以最通俗移动的详细代码,告诉用户如何构建轮播图。

第一步:搭建框架

body>
    
    div class="overall">
        
        div class="content">
            
            ul>
                li>img src="./imgs/1.jpg" alt="">li>
                li>img src="./imgs/2.jpg" alt="">li>
                li>img src="./imgs/3.jpg" alt="">li>
            ul>
            
            ol>
                li>li>
                li>li>
                li>li>
            ol>
        div>        
    div>
body>

第二步:CSS 渲染

style>
    
    .overall{
        
        width: 900px;
        height: 500px;

        
        align-items: center;

        
        margin: 5% auto;        
    }

    .content{
        
        position: relative;
        height: 400px;
    }

    .content ul{
        
        list-style-type: none;
    }

    .content ul>li{
        width: 600px;
        height: 300px;

        
        position: absolute;

        
        transition: 1s;

        
        opacity: 0;
    }
    
    
    .content ul>li img{    
        
        width: 900px;
        height: 500px;

        
        border-radius: 5%;
     
        
        border: 5px solid #0e0600;        
    }

    
    .content ol{
        
        position: relative;

        
        display: grid;
        
        
        grid-template-columns: repeat(3, 75px);

        
        grid-template-rows: auto;

        
        
        grid-gap: 1em;
        gap:1em;

        
        float: right;

        
        margin-top: 450px;

        
        list-style: none;

        
        top: 0;

        
        left: 0;
    }

    .content ol li{
        
        width: 25px;
        height: 10px;

        
        font-size: 15px;

        
        line-height: 20px;

        
        float: left;

        
        text-align: center;

        
        border-radius: 2em;

        
        border: 5px solid #af9d9d;
    }
style>

第三步:JS 逻辑

script>
    
    window.onload = function()
    {

    
    var content = this.document.getElementsByClassName("content")[0];

    
    var li = content.getElementsByTagName("li");

    
    function fun(i, j)
    {
        li[i].style.opacity=1;
        li[j].style.opacity=0;

        
        
        
        li[i + 3].style.backgroundColor = "#ffffff";

        
        li[j + 3].style.backgroundColor = "#00000000";
    }

    
    fun(0, 1);
    var i = 0;
    function auto()
    {
        i ++;
        
        if(i >= 3)
        {
        
            i = 0;
            
            
            fun(0, 2);
        }
        else
        {
            
            fun(i, i - 1);
        } 
    }
    
    timer = this.setInterval(auto, 2000);

    
    content.onmouseover = function () 
    { 
        
        clearInterval(timer);
    }

    
    content.onmouseout = function () 
    { 
        
        timer = setInterval(auto, 2000); 
    }
    var j = 0;
    for(; j  3; j++)
    {
        
        li[j + 3].index = j;
        
        li[j + 3].onclick = function()
        {
            fun(this.index, i)
            i = this.index;
        }
    }
}
script>

运行结果
html 轮播图

代码汇总

DOCTYPE html>
html lang="en">

head>
    meta charset="UTF-8">
    meta http-equiv="X-UA-Compatible" content="IE=edge">
    meta name="viewport" content="width=device-width, initial-scale=1.0">
    title>Apple14 介绍 title>
    style>
        
        .overall{
            
            width: 900px;
            height: 500px;
    
            
            align-items: center;
    
            
            margin: 5% auto;        
        }
    
        .content{
            
            position: relative;
            height: 400px;
        }
    
        .content ul{
            
            list-style-type: none;
        }
    
        .content ul>li{
            width: 600px;
            height: 300px;
    
            
            position: absolute;
    
            
            transition: 1s;
    
            
            opacity: 0;
        }
        
        
        .content ul>li img{    
            
            width: 900px;
            height: 500px;
    
            
            border-radius: 5%;
         
            
            border: 5px solid #0e0600;        
        }
    
        
        .content ol{
            
            position: relative;
    
            
            display: grid;
            
            
            grid-template-columns: repeat(3, 75px);
    
            
            grid-template-rows: auto;
    
            
            
            grid-gap: 1em;
            gap:1em;
    
            
            float: right;
    
            
            margin-top: 450px;
    
            
            list-style: none;
    
            
            top: 0;
    
            
            left: 0;
        }
    
        .content ol li{
            
            width: 25px;
            height: 10px;
    
            
            font-size: 15px;
    
            
            line-height: 20px;
    
            
            float: left;
    
            
            text-align: center;
    
            
            border-radius: 2em;
    
            
            border: 5px solid #af9d9d;
        }
    style>
    
head>

body>
    
    div class="overall">
        
        div class="content">
            
            ul>
                li>img src="./imgs/1.jpg" alt="">li>
                li>img src="./imgs/2.jpg" alt="">li>
                li>img src="./imgs/3.jpg" alt="">li>
            ul>
            
            ol>
                li>li>
                li>li>
                li>li>
            ol>
        div>        
    div>
body>

script>
    
    window.onload = function()
    {

    
    var content = this.document.getElementsByClassName("content")[0];

    
    var li = content.getElementsByTagName("li");

    
    function fun(i, j)
    {
        li[i].style.opacity=1;
        li[j].style.opacity=0;

        
        
        
        li[i + 3].style.backgroundColor = "#ffffff";

        
        li[j + 3].style.backgroundColor = "#00000000";
    }

    
    fun(0, 1);
    var i = 0;
    function auto()
    {
        i ++;
        
        if(i >= 3)
        {
        
            i = 0;
            
            
            fun(0, 2);
        }
        else
        {
            
            fun(i, i - 1);
        } 
    }
    
    timer = this.setInterval(auto, 2000);

    
    content.onmouseover = function () 
    { 
        
        clearInterval(timer);
    }

    
    content.onmouseout = function () 
    { 
        
        timer = setInterval(auto, 2000); 
    }
    var j = 0;
    for(; j  3; j++)
    {
        
        li[j + 3].index = j;
        
        li[j + 3].onclick = function()
        {
            fun(this.index, i)
            i = this.index;
        }
    }
}
script>

html>

如果大家觉得有用的话,可以关注我下面的微信公众号,极客李华,我会在里面更新更多行业资讯,企业面试内容,编程资源,如何写出可以让大厂面试官眼前一亮的简历,让大家更好学习编程,我的抖音,B 站也叫极客李华。

原文地址: html 轮播图

    正文完
     0
    Yojack
    版权声明:本篇文章由 Yojack 于2024-09-30发表,共计3509字。
    转载说明:
    1 本网站名称:优杰开发笔记
    2 本站永久网址:https://yojack.cn
    3 本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长进行删除处理。
    4 本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
    5 本站所有内容均可转载及分享, 但请注明出处
    6 我们始终尊重原创作者的版权,所有文章在发布时,均尽可能注明出处与作者。
    7 站长邮箱:laylwenl@gmail.com
    评论(没有评论)