jQuery实现幻灯片播放效果

jQuery实现一个类似幻灯片播放的效果
主要功能:

  1. 幻灯片播放与暂停
  2. 点击前后按钮切换图片
  3. 点击按钮查看图片缩略图
  4. 点击对应的缩略图可以切换当前图片
  5. 缩略图可以进行翻页

效果:在这里插入图片描述
在这里插入图片描述
具体实现代码中已经详细解释

<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style type="text/css">
        * {
            margin: 0px;
            padding: 0px;
        }
        
        body {
            display: flex;
            justify-content: center;
            align-items: center;
        }
        
        #main_window {
            position: relative;
            width: 600px;
            height: 600px;
            margin: 10px;
            border-radius: 10px 10px 10px 10px;
            background: rgb(16, 16, 16);
            overflow: hidden;
        }
        
        #show_window {
            position: relative;
            top: 75px;
            left: 75px;
            width: 450px;
            height: 450px;
        }
        
        #show_image {
            width: 450px;
            height: 450px;
            box-shadow: 1px 5px 5px #000;
        }
        
        #ctrl_window {
            position: absolute;
            top: 545px;
            left: 620px;
            width: 170px;
            height: 25px;
            padding: 5px;
            display: flex;
            justify-content: space-around;
            align-items: center;
            background-color: #000;
            border-radius: 5px 5px 5px 5px;
        }
        
        #ctrl_window .ctrl_btn {
            width: 25px;
            height: 25px;
            opacity: 0.5;
            transition: all 500ms ease;
        }
        
        #ctrl_window .ctrl_btn:hover {
            opacity: 1.0;
        }
        
        #more_btn {
            background-size: cover;
            background-image: url(https://img.php.cn/upload/course/000/000/006/58217dd1e1d07217.png);
        }
        
        #left_btn {
            background-size: cover;
            background-image: url(https://img.php.cn/upload/course/000/000/006/58217e1322f73540.png);
        }
        
        #ctrl_window .pause {
            background-size: cover;
            background-image: url(https://img.php.cn/upload/course/000/000/006/58217e6fceab9946.png);
        }
        
        #ctrl_window .play {
            background-size: cover;
            background-image: url(https://img.php.cn/upload/course/000/000/006/58217e8f030de682.png);
        }
        
        #right_btn {
            background-size: cover;
            background-image: url(https://img.php.cn/upload/course/000/000/006/58217e4c9f3df621.png);
        }
        
        #preview_images_window {
            position: absolute;
            display: flex;
            justify-content: center;
            align-items: center;
            top: -250px;
            left: 125px;
            width: 350px;
            height: 250px;
            border-radius: 10px 10px 10px 10px;
            opacity: 1;
            background-color: rgba(0, 0, 0, 0.8);
        }
        
        .image_group {
            position: absolute;
            width: 285px;
            height: 188px;
            opacity: 1;
            /* transition: all 300ms ease; */
        }
        
        #preview_images_window .small_image {
            width: 85px;
            height: 85px;
            margin: 5px;
            opacity: 0.5;
            float: left;
            transition: all 300ms ease;
        }
        
        #preview_images_window .small_image:hover {
            opacity: 1.0;
        }
        
        .preview_ctrl_btn {
            position: absolute;
            width: 20px;
            height: 20px;
            background-repeat: no-repeat;
            background-position-x: center;
            background-position-y: center;
            opacity: 0.5;
            transition: all 300ms ease;
        }
        
        .preview_ctrl_btn:hover {
            opacity: 1.0;
        }
        
        #previous_btn {
            left: 10px;
            background-image: url(https://img.php.cn/upload/course/000/000/006/58217ef19a95e568.png);
        }
        
        #next_btn {
            right: 10px;
            background-image: url(https://img.php.cn/upload/course/000/000/006/58217ecb6c91f719.png);
        }
        
        #close_btn {
            border-top-left-radius: 7px;
            border-top-right-radius: 7px;
            width: 50px;
            height: 20px;
            bottom: 0px;
            background-color: #202020;
            background-image: url(https://img.php.cn/upload/course/000/000/006/58217f13d5601331.png);
        }
    </style>
</head>

<body>
    <div id="main_window">
        <div id="show_window">
            <img src="images/img1.jpg" id="show_image">
        </div>
        <div id="ctrl_window">
            <a id="more_btn" class="ctrl_btn"></a>
            <a id="left_btn" class="ctrl_btn"></a>
            <a id="switch_btn" class="ctrl_btn pause"></a>
            <a id="right_btn" class="ctrl_btn"></a>
        </div>
        <div id="preview_images_window">
            <div class="image_group">
                <img src="images/img1.jpg" class="small_image">
                <img src="images/img2.jpg" class="small_image">
                <img src="images/img3.jpg" class="small_image">
                <img src="images/img4.jpg" class="small_image">
                <img src="images/img5.jpg" class="small_image">
                <img src="images/img6.jpg" class="small_image">
            </div>
            <div class="image_group">
                <img src="images/img7.jpg" class="small_image">
                <img src="images/img8.jpg" class="small_image">
                <img src="images/img9.jpg" class="small_image">
                <img src="images/img10.jpg" class="small_image">
                <img src="images/img11.jpg" class="small_image">
                <img src="images/img12.jpg" class="small_image">
            </div>
            <div class="image_group">
                <img src="images/img13.jpg" class="small_image">
                <img src="images/img14.jpg" class="small_image">
                <img src="images/img15.jpg" class="small_image">
                <img src="images/img16.jpg" class="small_image">
                <img src="images/img17.jpg" class="small_image">
                <img src="images/img18.jpg" class="small_image">
            </div>
            <a id="previous_btn" class="preview_ctrl_btn"></a>
            <a id="next_btn" class="preview_ctrl_btn"></a>
            <a id="close_btn" class="preview_ctrl_btn"></a>
        </div>
    </div>
    <script type="text/javascript" src="jquery-3.5.1.min.js"></script>
    <script type="text/javascript">
        $(function() {
            //获取组件:主窗口
            var $main_window = $("#main_window");
            //获取组件:控制按钮窗口
            var $ctrl_window = $("#ctrl_window");
            //获取组件:主窗口显示图片
            var $show_image = $("#show_image");
            //获取组件:预览窗口
            var $preview_window = $("#preview_images_window");
            //获取组件:预览图片组
            var $image_groups = $(".image_group");
            //获取组件:主窗口控制按钮
            var $more_btn = $("#more_btn");
            var $left_btn = $("#left_btn");
            var $right_btn = $("#right_btn");
            var $switch_btn = $("#switch_btn");
            //获取组件:预览窗控制按钮
            var $previous_btn = $("#previous_btn");
            var $next_btn = $("#next_btn");
            var $close_btn = $("#close_btn");

            //设置变量:图片组的数量
            var groups_size = $image_groups.length;
            //设置变量:当前图片组索引
            var current_group_index = 0;
            //设置变量:图片组中的图片数量
            var images_size = $image_groups.first().children().length;
            //设置变量:当前图片组的图片索引
            var current_image_index = 0;
            //设置变量:当前播放状态:true播放,false暂停
            var current_play_status = true;
            //设置变量:定时器
            var timeout = setInterval(showNextImage, 5000);

            //初始化图片组隐藏和显示第一组
            $image_groups.hide();
            $image_groups.first().show();

            //绑定事件:主窗口控制台区域的弹出
            mainWindowMouseenter();
            //绑定事件:主窗口控制台区域的收起
            mainWindowMouseleave();
            //绑定事件:预览窗口开启按钮,停止轮播,同时主窗口控制台收起,并取消其的弹出事件
            $more_btn.bind("click", function() {
                current_play_status = false;
                $switch_btn.removeClass("pause");
                $switch_btn.addClass("play");
                clearInterval(timeout);
                previewWindowOpen();
                ctrlWindowDisappear();
                $main_window.unbind("mouseenter");
            });
            //绑定事件:预览窗口关闭按钮,同时主窗口控制台弹出,并再次绑定其的弹出事件
            $close_btn.bind("click", function() {
                previewWindowClose();
                ctrlWindowAppear();
                mainWindowMouseenter();
            });
            //绑定事件:展示上一组预览图
            $previous_btn.bind("click", function() {
                if (!($image_groups.is(":animated"))) {
                    showPreviewImageGroup();
                };
            });
            //绑定事件:展示下一组预览图
            $next_btn.bind("click", function() {
                if (!($image_groups.is(":animated"))) {
                    showNextImageGroup();
                }
            });
            //绑定事件:展示上一张图片
            $left_btn.bind("click", function() {
                if (!($show_image.is(":animated"))) {
                    showPreviewImage();
                }
            });
            //绑定事件:展示下一张图片
            $right_btn.bind("click", function() {
                if (!($show_image.is(":animated"))) {
                    showNextImage();
                }
            });
            //绑定事件:预览图点击显示图片
            $image_groups.each(function() {
                var $this = $(this);
                $this.children().each(function(i) {
                    $(this).bind("click", function() {
                        showImageByIndex(i);
                        $close_btn.trigger("click");
                    });
                });
            });
            //绑定事件:播放与暂停图片的切换
            $switch_btn.bind("click", function() {
                var $this = $(this);
                $this.toggleClass("play");
                $this.toggleClass("pause");
                if (current_play_status) {
                    current_play_status = false;
                    clearInterval(timeout)
                } else {
                    current_play_status = true;
                    timeout = setInterval(showNextImage, 5000);
                }
            });
            //主窗口mouseenter绑定事件:控制台的弹出
            function mainWindowMouseenter() {
                $main_window.bind("mouseenter", function() {
                    ctrlWindowAppear();
                });
            }
            //主窗口mouseleave绑定事件:控制台的收起
            function mainWindowMouseleave() {
                $main_window.bind("mouseleave", function() {
                    ctrlWindowDisappear();
                });
            }

            //控制按钮窗口出现
            function ctrlWindowAppear() {
                $ctrl_window.stop().animate({
                    left: ($main_window.width() - $ctrl_window.width() - 20) + "px"
                }, 500, "swing")
            }
            //控制按钮窗口关闭
            function ctrlWindowDisappear() {
                $ctrl_window.stop().animate({
                    left: ($main_window.width() + 20) + "px"
                }, 500, "swing");
            }
            //收起预览窗口
            function previewWindowClose() {
                $preview_window.animate({
                    top: (-$preview_window.height()) + "px"
                }, 500, "swing");
            }
            //弹出预览窗口
            function previewWindowOpen() {
                $preview_window.animate({
                    top: "0px"
                }, 500, "swing");
            }
            //淡入淡出展示上一组预览图片
            function showPreviewImageGroup() {
                if (current_group_index != 0) {
                    var now = current_group_index;
                    current_group_index--;
                    var preview = current_group_index;
                    $image_groups.eq(now).fadeOut(function() {
                        $image_groups.eq(preview).fadeIn();
                    });
                }
            }
            //淡入淡出展示下一组预览图片
            function showNextImageGroup() {
                if (current_group_index != groups_size - 1) {
                    var now = current_group_index;
                    current_group_index++;
                    var next = current_group_index;
                    $image_groups.eq(now).fadeOut(function() {
                        $image_groups.eq(next).fadeIn();
                    });
                }
            }
            //设置展示index组预览图片
            function showImageGroupByIndex(index) {
                $image_groups.eq(current_group_index).hide();
                $image_groups.eq(index).show();
                current_group_index = index;
            }
            //通过index设置图片
            function showImageByIndex(index) {
                var $small_image = $image_groups.eq(current_group_index).find(".small_image").eq(index);
                var source = $small_image.attr("src");
                $show_image.fadeOut(function() {
                    $show_image.attr("src", source);
                    $show_image.fadeIn();
                    current_image_index = index;
                });
            }
            //主窗口淡入淡出展示上一个图片
            function showPreviewImage() {
                if (current_image_index == 0) {
                    showImageGroupByIndex(current_group_index == 0 ? groups_size - 1 : current_group_index - 1);
                    showImageByIndex(images_size - 1);
                } else {
                    showImageByIndex(current_image_index - 1);
                }
            }
            //主窗口淡入淡出展示下一个图片
            function showNextImage() {
                if (current_image_index == images_size - 1) {
                    showImageGroupByIndex(current_group_index == groups_size - 1 ? 0 : current_group_index + 1);
                    showImageByIndex(0);
                } else {
                    showImageByIndex(current_image_index + 1);
                }
            }
        });
    </script>
</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值