原生js按钮控制图片切换代码



108 428 143



特效描述:原生js按钮控制 图片切换,原生js基于css3属性制作三种图片切换效果,3D轮播、无缝轮播 、淡入淡出图片轮播任意切换代码。

代码结构

1. HTML代码

    <div class="banner">
        <div class="banner-view1">
            <ul class="banner-pic1">
                <li class="pic0 pic11"></li>
                <li class="pic0 pic22"></li>
                <li class="pic0 pic33"></li>
                <li class="pic0 pic44"></li>
                <li class="pic0 pic55"></li>
                <li class="pic0 pic66"></li>
            </ul>
        </div>
        <span id="prev"></span>
        <span id="next"></span>
        <ul class="indicators">
            <li class="indicator"></li>
            <li class="indicator"></li>
            <li class="indicator"></li>
            <li class="indicator"></li>
            <li class="indicator"></li>
            <li class="indicator"></li>
        </ul>
    </div>
    <div class="control">
        <button id="btn1">3D轮播</bttton>
        <button id="btn2">无缝轮播(默认)</bttton>
        <button id="btn3">淡入淡出轮播</button>
    </div>
    <script>
        var oPrev = document.getElementById("prev"),
            oNext = document.getElementById("next"),
            oPicbox = document.getElementsByClassName("banner-pic")[0],
            oBall = document.getElementsByClassName("indicator"),
            oBtn1 = document.getElementById("btn1"),
            oBtn2 = document.getElementById("btn2"),
            oBtn3 = document.getElementById("btn3"),
            oBannerPic1 = document.getElementsByClassName("banner-pic1")[0];
            oLis = oBannerPic1.getElementsByTagName("li"),
            oBannerView1 = document.querySelector(".banner-view1"),
            length = oLis.length;
        var className1 = [
                "pic1",
                "pic2",
                "pic3",
                "pic4",
                "pic5",
                "pic6"
            ],
            className2 = [
                "pic11",
                "pic22",
                "pic33",
                "pic44",
                "pic55",
                "pic66"
            ],
            className3 = [
                "pic111",
                "pic222",
                "pic333",
                "pic444",
                "pic555",
                "pic666"
            ];
        var xDeg = [0,360,180,360,270,90],   //X轴方向旋转的度数
            yDeg = [0,-90,0,90,0,0], //Y轴方向旋转的度数
            xTranslate = [0,300,600,900,1200,1500], //X轴方向的位移
            i = 1; //串起整个思路的全局变量
        oBall[i-1].style.backgroundColor = '#ccc'; //默认最下面的第一个小球变色
        oBtn1.onclick = function () {
            oBannerView1.className = "banner-view";
            oBannerPic1.className = "banner-pic";
            oBannerPic1.style.cssText = "";
            for(let i=0; i<length; i++){
                oLis[i].className = `pic ${className1[i]}`;
                oLis[i].style.opacity = '1';
            }
            change1();
            arrow1();
            oBannerPic1.style.cssText = 'transform: rotateX('+xDeg[i-1]+'deg)'+'rotateY('+yDeg[i-1]+'deg)';
        };
        oBtn2.onclick = function () {
            oBannerView1.className = "banner-view1";
            oBannerPic1.className = "banner-pic1";
            oBannerPic1.style.cssText = "";
            for(let i=0; i<length; i++){
                oLis[i].className = `pic0 ${className2[i]}`;
                oLis[i].style.opacity = '1';
            }
            change2();
            arrow2();
            oBannerPic1.style.cssText = `margin-left: ${-xTranslate[i-1]}px;`;
        }
        oBtn3.onclick = function () {
            oBannerView1.className = "banner-view11";
            oBannerPic1.className = "banner-pic11";
            oBannerPic1.style.cssText = "";
            for(let i=0; i<length; i++){
                oLis[i].className = `pic00 ${className3[i]}`;
                oLis[i].style.opacity = '0';
            }
            change3();
            arrow3();
            oLis[i-1].style.opacity = `1`;
        }
        function arrow1 () {  //3D轮播左右箭头方法
            oPrev.onclick = function () { 
                oBall[i-1].style.backgroundColor = '#f5eedc';
                i--;
                if(i < 1){
                    i = 6; 
                }
                oBannerPic1.style.cssText = 'transform: rotateX('+xDeg[i-1]+'deg)'+'rotateY('+yDeg[i-1]+'deg)';
                oBall[i-1].style.backgroundColor = '#ccc';
            }
            oNext.onclick = function () { //右箭头相同 - - 我就不说了
                oBall[i-1].style.backgroundColor = '#f5eedc';
                i++;
                if(i > 6){
                    i = 1;
                }
                oBannerPic1.style.cssText = 'transform: rotateX('+xDeg[i-1]+'deg)'+'rotateY('+yDeg[i-1]+'deg)';
                oBall[i-1].style.backgroundColor = '#ccc';
            };
        }
        arrow2();
        function arrow2() {  //无缝轮播左右箭头方法
            oPrev.onclick = function () { 
                oBall[i-1].style.backgroundColor = '#f5eedc';
                i--;
                if(i < 1){
                    i = 6; 
                }
                oBannerPic1.style.cssText = `margin-left: ${-xTranslate[i-1]}px;`;
                oBall[i-1].style.backgroundColor = '#ccc';
            }
            oNext.onclick = function () {
                oBall[i-1].style.backgroundColor = '#f5eedc';
                i++;
                if(i > 6){
                    i = 1;
                }
                oBannerPic1.style.cssText = `margin-left: ${-xTranslate[i-1]}px;`;
                oBall[i-1].style.backgroundColor = '#ccc';
            };
        }
        function arrow3() { //淡入淡出轮播左右箭头方法
            oPrev.onclick = function () { 
                oBall[i-1].style.backgroundColor = '#f5eedc';
                oLis[i-1].style.opacity = "0";
                i--;
                if(i < 1){
                    i = 6; 
                }
                oLis[i-1].style.opacity = "1";
                oBall[i-1].style.backgroundColor = '#ccc';
            }
            oNext.onclick = function () { 
                oBall[i-1].style.backgroundColor = '#f5eedc';
                oLis[i-1].style.opacity = "0";
                i++;
                if(i > 6){
                    i = 1;
                }
                oLis[i-1].style.opacity = "1";
                oBall[i-1].style.backgroundColor = '#ccc';
            };
        }
/*      单边和循环模式开发
        oPrev.onclick = function () { //左箭头的点击事件
            oBall[i-1].style.backgroundColor = '#f5eedc'; //这是为了上个小圆点亮起时这个小圆点恢复原来的颜色
            i--; //表示往上一个移动
            if(i < 1){
                if(bool){
                    i = 6; //当i=1时还点击左箭头上移 将会把6赋给i 并把6的角度赋给XY
                }
                else {
                    i = 1;
                    alert("前面已经没有啦!");
                }
            }
            oPicbox.style.transform = 'rotateX('+ xDeg[i-1] +'deg)'+'rotateY('+ yDeg[i-1] +'deg)'; //实现让立方体旋转
            oBall[i-1].style.backgroundColor = '#ccc'; //让下方对应的新的小圆点亮起
        };
        oNext.onclick = function () { //右箭头相同 - - 我就不说了
            oBall[i-1].style.backgroundColor = '#f5eedc';
            i++;
            if(i > 6){
                if(bool){
                    i = 1;
                }
                else {
                    i = 6;
                    alert("后面已经没有啦!");
                }
            }
            oPicbox.style.transform = 'rotateX('+xDeg[i-1]+'deg)'+'rotateY('+yDeg[i-1]+'deg)';
            oBall[i-1].style.backgroundColor = '#ccc';
        };
*/      
        function change1 () { //3D轮播下方小球方法
            for(let j=0; j<6; j++){
                oBall[j].onmouseenter = function () {
                    i = j+1;
                    oBannerPic1.style.transform = 'rotateX('+xDeg[j]+'deg)'+'rotateY('+yDeg[j]+'deg)';
                    console.log(xDeg[j]);
                    console.log(yDeg[j]);
                    for (let t = 0; t < 6; t++ ) {
                        if(t+1 === i){
                            oBall[t].style.backgroundColor = '#ccc';
                            continue;
                        }
                        oBall[t].style.backgroundColor = '#f5eedc';
                    }
                }
            }
        }
        change2();
        function change2() { //无缝轮播下方小球方法
            for(let j=0; j<6; j++){
                oBall[j].onmouseenter = function () {
                    i = j+1;
                    oBannerPic1.style.marginLeft = `${-xTranslate[j]}px`;
                    for (let t = 0; t < 6; t++ ) {
                        if(t+1 === i){
                            oBall[t].style.backgroundColor = '#ccc';
                            continue;
                        }
                        oBall[t].style.backgroundColor = '#f5eedc';
                    }
                }
                console.log(i);
            }
        }
        function change3() { //淡入淡出轮播下方小球方法
            for(let j = 0; j<6; j++){
                oBall[j].onmouseenter = function () {
                    i = j+1;
                    oLis[j].style.opacity = `1`;
                    for (let t = 0; t < 6; t++ ) {
                        if(t+1 === i){
                            oBall[t].style.backgroundColor = '#ccc';
                            oLis[t].style.opacity = `1`;
                            continue;
                        }
                        oBall[t].style.backgroundColor = '#f5eedc';
                        oLis[t].style.opacity = '0';
                    }
                }
            }
        }
    </script>



用户评论
大牛,别默默的看了,快登录帮我点评一下吧!:)      登录 | 注册


热门标签: 滚动切换 滚动条切换 滑动选项卡 滑动切换 选项卡切换 选项卡 切换 tab切换 页面切换 选项卡插件 切换插件 滑动手风琴 旋转翻转 旋转 翻转 渐隐切换 淡出淡进 淡出 淡进 切换按钮 表单 表单美化 表单插件 表单美化插件 图片翻转 图片旋转 图片淡出淡进 图片淡出 图片淡进 图片切换 图片选项卡 图标选项卡 选项卡自动切换 渐隐切换 按钮控制

×
×

注册

官方QQ群

扫描上面二维码加微信群

官方QQ群

jQuery/js讨论群
群号:642649996
Css3+Html5讨论群
群号:322131262

加群请备注:从官网了解到