js页面切换

<style>
    * {
        margin: 0;
        padding: 0;
    }

    #title {
        overflow: hidden;
        margin: 100px auto;
        width: 900px;
        list-style: none;
    }

    #title li {
        float: left;
        width: 300px;
        height: 50px;
        text-align: center;
        line-height: 50px;
        font-size: 30px;
        background-color: blue;
    }

    /* #title li:first-child {
        background-color: gold;
    } */

    #contents {
        list-style: none;
        width: 900px;
        margin: 50px auto;
    }

    #contents li {
        width: 900px;
        height: 500px;
        text-align: center;
        line-height: 500px;
        font-size: 50px;
    }

    #contents li:first-child {
        background-color: aqua;
    }

    #contents li:nth-child(2) {
        background-color: aquamarine;
        display: none;
    }

    #contents li:last-child {
        background-color: antiquewhite;
        display: none;
    }
</style>
<body>
    <div>
        <ul id="title">
            <li>标题一</li>
            <li>标题二</li>
            <li>标题三</li>
        </ul>
    </div>
    <div>
        <ul id="contents">
            <li>内容一</li>
            <li>内容二</li>
            <li>内容三</li>
        </ul>
    </div>
<script>
    var title = document.getElementById('title').getElementsByTagName('li')
    var contents = document.getElementById('contents').getElementsByTagName('li')
    for (let i = 0; i < title.length; i++) {
        title[i].onclick = function () {
            for (let j = 0; j < contents.length; j++) {
                if (i == j) {
                    this.style.backgroundColor = 'gold';
                    contents[j].style.display = "block"
                } else {
                    title[j].style.backgroundColor = 'blue';
                    contents[j].style.display = 'none';
                }
            }
        }
    }
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现网页左右切换效果的JavaScript代码主要分为两部分: 1. 控制页面布局:需要将两个页面放置在一个容器内,并使用CSS来控制它们的排列方式。具体可以使用float、position、flex等方式来实现。 2. 控制页面切换:需要监听用户的鼠标点击或触摸事件,并根据用户的操作来改变页面的位置。具体可以使用DOM操作来改变页面的样式,如改变元素的位置、宽度、高度等。 下面是一个简单的示例代码,实现了通过点击左右箭头来切换页面的效果: HTML部分: ``` <div id="container"> <div class="page">第一页内容</div> <div class="page">第二页内容</div> </div> <div id="prev">←</div> <div id="next">→</div> ``` CSS部分: ``` #container { width: 100%; height: 100%; position: relative; white-space: nowrap; } .page { width: 100%; height: 100%; display: inline-block; vertical-align: top; } #prev, #next { position: absolute; top: 50%; transform: translateY(-50%); font-size: 30px; cursor: pointer; } #prev { left: 20px; } #next { right: 20px; } ``` JavaScript部分: ``` var container = document.getElementById('container'); var pages = container.getElementsByClassName('page'); var prevButton = document.getElementById('prev'); var nextButton = document.getElementById('next'); var currentPage = 0; function switchPage(direction) { if (direction === 'prev') { currentPage = (currentPage - 1 + pages.length) % pages.length; } else if (direction === 'next') { currentPage = (currentPage + 1) % pages.length; } container.style.transform = 'translateX(-' + currentPage * 100 + '%)'; } prevButton.addEventListener('click', function() { switchPage('prev'); }); nextButton.addEventListener('click', function() { switchPage('next'); }); ``` 这样就可以实现一个简单的网页左右切换效果了。当然,实际应用中还需要考虑更多的细节,如动画效果、响应式布局、移动端兼容等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值