• html中使用JS实现图片轮播效果


    1.首先是效果图,要在网页中实现下图的轮播效果,有四张图片,每张图片有自己的标题,然后还有右下角的小方框,鼠标悬浮在小方框上,会切换到对应的图片中去。

    2.先是HTML中的内容,最外层是轮播图整个的容器“slideShowContainer”,里边是用来装图片的“picUl”和用来显示小方框的“dotUl”,以及用来装标题的“titleDiv”。

    <div id="slideShowContainer">
    <ul id="picUl">
    <li><a href="#"><img src="img/lunbo1.jpg" alt=""/></a></li>
    <li><a href="#"><img src="img/lunbo2.jpg" alt=""/></a></li>
    <li><a href="#"><img src="img/lunbo3.jpg" alt=""/></a></li>
    <li><a href="#"><img src="img/lunbo4.jpg" alt=""/></a></li>
    </ul>
    <ul id="dotUl">
    <li class="selected">1</li>
    <li class="unselected">2</li>
    <li class="unselected">3</li>
    <li class="unselected">4</li>
    </ul>
    <div id="titleDiv">
    <span class="show"><a href="#">党政机关公务用车有了统一标识</a></span>
    <span class="hide"><a href="#">“洛阳创新”亮相第52届巴黎航展</a></span>
    <span class="hide"><a href="#">中国河洛乡愁摄影主题公园揭牌</a></span>
    <span class="hide"><a href="#">洛阳机场建成生态停车场</a></span>
    </div>
    </div>

    3.然后是css中的样式
    #slideShowContainer{
    425px;
    height: 325px;
    margin-top: 10px;
    margin-left: 10px;
    overflow: hidden;
    position: relative;
    }
    #slideShowContainer img{
    425px;
    height: 325px;
    transition: all 0.6s;
    }
    #slideShowContainer img:hover{
    transform: scale(1.07);
    }
    #picUl{
    list-style: none;
    }
    #dotUl{    
    list-style: none;
    display: flex;
    flex-direction: row;
    position: absolute;  //使用绝对布局,固定于左下角
    right: 21px;
    bottom: 15px;
    z-index: 2;  //通过设置z-index的值大于#titleDiv中z-index的值,使其浮在标题栏的上方
    }
    #titleDiv{
    position: absolute;
    100%;
    height: 42px;
    bottom: 0px;
    left: 0px;
    background-color: #000000;
    opacity:0.6;  //设置透明度,实现标题栏半透明效果
    z-index: 1;
    }
    #titleDiv>span{
    line-height: 42px;
    color: #FFFFFF;
    margin-left: 20px;
    270px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    }
    #titleDiv>span>a{
    color: #FFFFFF;
    }
    .selected{
    12px;
    height: 12px;
    background-color: #FFFFFF;
    color: transparent;
    margin-left: 9px;
    }
    .unselected{
    12px;
    height: 12px;
    background-color: #0069AD;
    color: transparent;
    margin-left: 9px;
    }
    .hide{
    display: none;
    }
    .show{
    display: block;
    }
    4.通过js控制,动态修改相应的样式,达到图片轮播的效果
    /*图片轮播*/
    var slideShowContainer = document.getElementById("slideShowContainer");
    var pic = document.getElementById("picUl").getElementsByTagName("li");
    var dot = document.getElementById("dotUl").getElementsByTagName("li");
    var title = document.getElementById("titleDiv").getElementsByTagName("span");
    var index = 0;
    var timer = null;
    /*定义图片切换函数*/
    function changePic (curIndex) {
    for(var i = 0;i < pic.length;++i){
    pic[i].style.display = "none";
    dot[i].className = "unselected";
    title[i].className = "hide"
    }
    pic[curIndex].style.display = "block";
    dot[curIndex].className = "selected";
    title[curIndex].className = "show";
    }
    /*index超出图片总量时归零*/
    function autoPlay(){
    if(+index >= pic.length){
    index = 0;
    }
    changePic(index);
    index++;
    }
    /*定义并调用自动播放函数*/
    timer = setInterval(autoPlay,1500);
    /*鼠标划过整个容器时停止自动播放*/
    slideShowContainer.onmouseover = function(){
    clearInterval(timer);
    }
    /*鼠标离开整个容器时继续播放下一张*/
    slideShowContainer.onmouseout = function(){
    timer = setInterval(autoPlay,1500);
    }
    /*遍历所有数字导航实现划过切换至对应的图片*/
    for(var i = 0;i < dot.length;i++){
    dot[i].onmouseover = function(){
    clearInterval(timer);
    index = this.innerText-1;
    changePic(index)
    }
    }
     
  • 相关阅读:
    python笔记-2
    python笔记-1
    生成列表
    内置函数
    装饰器、包的导入
    python3 编码
    python3 初识函数
    python3 文件读写
    python3 流程控制
    python3 数据类型
  • 原文地址:https://www.cnblogs.com/wangxinqiang1995/p/7085120.html
Copyright © 2020-2023  润新知