纯js点选 切换图片


代码如下:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <style type="text/css">
        img
        {
            position: absolute;
            left: 50px;
            height: 290px;
            width: 300px;
        }

        #con
        {
            height: 300px;
            width: 400px;
        }

        #prev
        {
            background: url(image/prev.gif) 0 150px no-repeat;
            height: 100%;
            width: 7%;
            margin-top: auto 0;
            float: left;
        }

        #next
        {
            background: url(image/next.gif) 0 150px no-repeat;
            height: 100%;
            width: 7%;
            margin-top: auto 0;
            float: right;
        }

        #prev:hover
        {
            background: url(image/prev.gif) 0 150px no-repeat, rgba(221, 214, 214,0.4);
        }

        #next:hover
        {
            background: url(image/next.gif) 0 150px no-repeat, rgba(221, 214, 214,0.4);
        }
    </style>
</head>
<body>
    <div id="con">
        <div id="prev">上一张</div>
        <img src="images/1-1.png" id="3" />
        <img src="images/1-2.png" id="2" />
        <img src="images/2-1.png" id="1" />
        <div id="next">下一张</div>
    </div>
    <br />
    <div style="height: 100px; width: 100px; position: absolute; border: 1px solid red;"
        onclick="a(this)">
    </div>
    <script type="text/javascript">
        (function () {
            var gao = {
                prev: document.getElementById("prev"),
                next: document.getElementById("next"),
                img: document.getElementsByTagName("img"),
                current: 0,
                direction: 'no',
                count: document.getElementsByTagName("img").length,
                run: function (direction) {
                    if (direction == "next") {
                        if (gao.current < gao.count - 1) {
                            gao.current += 1;
                            gao.go(direction);
                        }
                    }
                    if (direction == "prev") {

                        if (gao.current > 0) {
                            gao.current -= 1;
                            gao.go(direction);
                        }
                    }
                },
                go: function (direction) {
                    if (direction == "prev") {
                        var cu = gao.count - gao.current - 1;
                        gao.img[cu].style.display = "block";
                        var prev = setInterval(function () {
                            if (parseInt(gao.img[cu].currentStyle.left, 10) > 50) {
                                gao.img[cu].style.left = parseInt(gao.img[cu].currentStyle.left, 10) - 10 + "px";
                            }
                            else { clearInterval(prev); }
                        }, 10);
                    }
                    else {
                        //  $("img:eq("+cu+")").animate({ left: '+=349px' }, 600, function () { img.style.display = "none"; });
                        var int = setInterval(function () {
                            var cu = gao.count - gao.current;
                            if (parseInt(gao.img[cu].currentStyle.left, 10) < 350) {
                                gao.img[cu].style.left = parseInt(gao.img[cu].currentStyle.left, 10) + 10 + "px";
                            }
                            else { gao.img[cu].style.display = "none"; clearInterval(int); }
                        }, 10);
                    }
                }
            };
            gao.next.addEventListener("click", function () { gao.run("next"); }, false);
            gao.prev.addEventListener("click", function () { gao.run("prev"); }, false);
        })();
    </script>
</body>
</html>
优质内容筛选与推荐>>
1、iOS pods集成与使用介绍
2、Delphi XE5 android 捕获几个事件
3、Linux 监控文件事件
4、巅峰极客 2019部分题解 writeup
5、学习内容用图片记录


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号