js/html/css实现轮播图

需求需要展示轮播主图和两侧图片,图片暂不需要自动轮播,点击图片放大展示在中间

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>轮播图</title>
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
 <style type="text/css">
     *{
 margin:0;
 padding:0;
}
.wrapper{
                position: relative;
                transform-style: preserve-3d;
                perspective: 800px;
                height: 300px;
                margin-top: 60px;
                margin-bottom: 60px;
            }
            .wrapper .active{
                border: 5px solid #FFFFFF;
                box-shadow: 0px 8px 39px 9px rgba(0, 0, 0, 0.3);
            }
            .rotation-img{
                position: absolute;
                left: 50%;
                top: 50%;
                width: 300px;
                height: 200px;
                margin-left: -150px;
                margin-top: -100px;
                border-radius: 8px;
                transition: transform 0.5s ease-in-out;
            }
 </style>
<script type="text/javascript">
var index=1,timer;

            $(document).ready(function(){
                window.onresize = function(){
                    console.log('onresize');
                    initalCarousel();
                }
                console.log(1111);
                var oImg = $('.rotation-img');
                // 默认中间展示索引值为0的这张图片
                var curDisplay = 0;
                // 将图片分散排列

                // 获得图片个数
                var len = oImg.length;

                // 定时器
                var timer;
                function init() {
                    console.log('init')
                    initalCarousel();
                    bindEvent();
                }
                init();

                function initalCarousel() {
                    // 获得中间图片
                    var hLen = Math.floor(oImg.length / 2);
                    // rNum,lNum分别是分散在中间图片左右左右两侧的图片索引
                    var rNum, lNum;
                    for (var i = 0; i < hLen; i++) {
                        lNum = curDisplay - i - 1;
                        console.log(oImg.eq(lNum).prevObject[0].offsetWidth)
                        // 分别让分散在左右两侧的图片平移旋转
                        const deviationWidth = oImg.eq(lNum).prevObject[0].offsetWidth; // 获取图片宽度
                        oImg.eq(lNum).css({
                            // transform: 'translateX(' + (-150 * (i + 1)) + 'px) translateZ(' + (200 - i * 100) + 'px) rotateY(30deg)'
                            transform: 'translateX(' + (-(deviationWidth + (deviationWidth/5))) + 'px) translateZ(' + 200 + 'px) rotateY(0deg)'
                        });
                        rNum = curDisplay + i + 1;
                        // 如果运动到最后一张 循环运动
                        if (rNum > oImg.length - 1) {
                            rNum -= oImg.length;
                        }
                        oImg.eq(rNum).css({
                            // transform: 'translateX(' + (150 * (i + 1)) + 'px) translateZ(' + (200 - i * 100) + 'px) rotateY(-30deg)'
                            transform: 'translateX(' + (deviationWidth + (deviationWidth/5))  + 'px) translateZ(' + 200 + 'px) rotateY(0deg)'
                        });
                        oImg.removeClass('active');
                    }

                    // 当前显示图片直接z轴向前移动 同时添加class名作为标记
                    oImg.eq(curDisplay).css({
                        transform: 'translateZ(300px)'
                    }).addClass('active');

                }
                // 点击事件
                function bindEvent() {
                    // 在每一张图片上绑定上点击事件
                    oImg.on('click', function (e) {
                        // 判断点击图片不是第一张显示图片
                        if (!$(this).hasClass('active')) {
                            // 标记图片flag切换
                            // 改变当前显示图片索引
                            curDisplay = $(this).index();
                            initalCarousel();
                        }
                        // 鼠标覆盖 清除自动轮播定时器
                    }).hover(function () {
                        clearInterval(timer);
                        // 鼠标移走 继续轮播
                    }, function () {
                        play();
                    });


                }

                // 自动播放
                function play() {
                    timer = setInterval(function () {
                         if (curDisplay == len - 1) {
                             curDisplay = 0;
                         } else {
                             curDisplay++;
                         }
                         initalCarousel();
                    }, 1000);
                }
});
 
</script>
</head>
<body>
	<div class="wrapper" id="wrap">
		<img src="https://img.nihaojewelry.com/product/2019/8/20/1205801301695401984_thumb_282x282/NHPF145217.jpg">
		<img src="https://img.nihaojewelry.com/product/2018/12/4/1205800176762097664_thumb_290x290/LL18120463536.jpg">
		<img src="https://img.nihaojewelry.com/product/2019/5/7/1205800827286065153_thumb_290x290/NHLL120570.jpg">
		<img src="https://img.nihaojewelry.com/product/2019/8/16/1205801268581371904_thumb_290x290/NHLN143542.jpg">
		<img src="https://img.nihaojewelry.com/product/2019/9/7/1205801410134937600_thumb_290x290/NHLN150815.jpg">
	 </div>

</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值