吸顶效果及水平轮播图

这个博客展示了如何创建一个带有吸顶效果的页面布局,并实现水平轮播图。当页面滚动超过一定距离时,顶部元素变为固定位置;同时,通过JavaScript实现了图片轮播功能,用户可以平滑浏览多张图片。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

</head>

<style>

    * {

        margin: 0;

        padding: 0;

    }

    .box {

        width: 1200px;

        height: 5000px;

        overflow: hidden;

        background-color: #ccc;

        margin: 0 auto;

    }

    .top {

        position: fixed;

        width: 1200px;

        height: 0;

        background-color: red;

        transition: height 1s;

    }

    #nav {

        position: fixed;

        width: 40px;

        text-align: center;

        line-height: 20px;

        background-color: aqua;

        right: 50%;

        margin-right: -640px;

        bottom: 100px;

    }

    .banner {

        width: 895px;

        height: 520px;

        margin: 300px auto 0;

        border: 1px #fff solid;

        overflow: hidden;

    }

    img {

        vertical-align: middle;

    }

</style>

<body>

    <div class="box">

        <div class="top"></div>

        <div id="nav">顶部</div>

        <div class="banner">

            <img src="img/1657022083345.jpg" alt="">

            <img src="img/1657790629109.jpg" alt="">

            <img src="img/1657790700592.jpg" alt="">

            <img src="img/1657790746440.jpg" alt="">

        </div>

    </div>

</body>

<script>

    var _top = document.querySelector('.top');

    var _nav = document.getElementById('nav');

    var timer = null

    window.onscroll = function () {

        var str = document.documentElement.scrollTop

        if (str >= 100) {

            _top.style.height = "100px";

        } else {

            _top.style.height = 0 + "px";

        }

    }

    _nav.onclick = function () {

        clearInterval(timer);

        timer = setInterval(() => {

            document.documentElement.scrollTop -= 10;

            var str = document.documentElement.scrollTop

            console.log(str);

            if (str === 0) {

                clearInterval(timer);

            }

        }, .1);

    }



 

    var _banner = document.querySelector('.banner');

    var _img = document.getElementsByTagName('img');

    _banner.innerHTML = _banner.innerHTML + _banner.innerHTML;

    var timer1 = null;

    function autoPlay() {

        clearInterval(timer1)

        timer1 = setInterval(function () {

            _banner.scrollTop++

            if (_banner.scrollTop >= _banner.scrollHeight / 2) {

                _banner.scrollTop = 0;

            }

            if (_banner.scrollTop % 519 === 0) {

                clearInterval(timer1);

                setTimeout(function () {

                    autoPlay();

                }, 1000);

            }

        }, 1)

    }

    autoPlay();

    _banner.onmouseover = function () {

        clearInterval(timer1)

    }

    _banner.onmouseout = function () {

        autoPlay()

    }

</script>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值