js 选项卡 轮播图

185 篇文章 0 订阅
32 篇文章 0 订阅

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style>
        * {
                margin: 0;
                padding: 0;
            }
            .box {
                margin: 100px auto;
                width: 600px;
                height: 400px;
                background: red;
                text-align: center
            }
            button {
                width: 60px;
                height:30px;                
            }
            input {
                width: 100px;
                height: 40px;
                font-size: 25px;
                color:green;
            }
            .box div {
                width: 100%;
                height: 400px;
                background: yellow;
                font-size: 30px;
                display: none;
            }
            .box .show {
                display: block;
            }
            .box .active {
                background: orange;
                width: 120px;
            }
        
        
        </style>
    </head>
    <body>
    <div class='box'>
    <button class="prev">&lt;&lt;</button>
    <input type="button" value="李白" class="active"/>
    <input type="button" value="杜甫" />
    <input type="button" value="苏轼" />
    <input type="button" value="白居易" />
    <button class="next">&gt;&gt;</button>
    <div class="show">
            凤凰台上凤凰游,
            凤去台空江自流。
            吴宫花草埋幽径,
            晋代衣冠成古丘。
            三山半落青天外,
            二水中分白鹭洲。
            总为浮云能蔽日,
            长安不见使人愁。
</div>
    <div>
           国破山河在,城春草木深。
           感时花溅泪,恨别鸟惊心。
           烽火连三月,家书抵万金。
           白头搔更短,浑欲不胜簪。
    
    </div>
    <div>
            水光潋滟晴方好,
            山色空蒙雨亦奇。
            欲把西湖比西子,
            淡妆浓抹总相宜。
    
    </div>
    <div>
            白浪茫茫与海连,
            平沙浩浩四无边。
            暮去朝来淘不住,
            遂令东海变桑田。
    
    </div>
    
    </div>
    

            
    </body>
    <script>
    var box = document.getElementsByClassName('box')[0]
    var prev = document.getElementsByClassName('prev')[0]
    var next = document.getElementsByClassName('next')[0]
    var aInput = document.getElementsByTagName('input')
    var aDiv = box.getElementsByTagName('div')
    var selectIndex = 0
    var count = aInput.length
    for (var i=0; i<count; i++) {
            aInput[i].index = i
            aInput[i].onclick = function () {
                refresh(this.index)
            }
        }
        
        var timer = setInterval(nextOption, 2000)
        
        box.onmouseover = function () {
            clearInterval(timer)
            timer = null
        }
        
        box.onmouseout = function () {
            timer = setInterval(nextOption, 2000)
        }
        
        prev.onclick = prevOption        
        next.onclick = nextOption
        
        
        function prevOption()
        {
            var index = selectIndex - 1
            if (index == -1) {
                index = count - 1
            }            
            refresh(index)
        }
        
        function nextOption()
        {
            var index = selectIndex + 1
            if (index == count) {
                index = 0
            }
            refresh(index)
        }
    
        function refresh(index)
        {
            if (index != selectIndex) {
                // 刷新内容
                // 清空所有显示
                for (var i=0; i<count; i++) {
                    aInput[i].className = ''
                    aDiv[i].className = ''
                }
                // 添加当前选中项
                aInput[index].className = 'active'
                aDiv[index].className = 'show'
                
                // 记录当前位置
                selectIndex = index
            }
        }

    </script>
    

</html>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值