前端自制Jquery插件————轮播

  昨天,自己心血来潮,弄了一个轮播图的插件,说来你们可能不信,这是我人生第一个插件,好,那我直接讲讲我的思路好了。

  首先,我以开发者的角度来看,一个好的插件的使用方式应该简单可靠,因为我做的是Jquery插件,所以最好的使用方式我觉得应该是,$(DOM).carousel(config)。其中DOM节点,用于填充轮播图片的地方,config是配置信息,包括是否循环,是否自动播放,播放时间间隔,图片地址等。因为时间短,我也只是简单实现了主要的功能。

  我第一件事情做的就是布局,没错,我是先把CSS样式以及布局写好的。布局简单的说就是要让所有图片在同一行显示,溢出的用overflow:hidden隐藏。我是这么做的,有一个div叫carousel,它的大小是和开发者提供的地方大小一样,也就是width: 100%,height:100%。并且要设置overflow:hidden。接着里面就放一个div叫carousel-content。这里面就是放图片的,它的高度是占满carousel的,但是它的宽度应该是所有图片宽度的总和,这是用js设置的。而所有图片都应该是float:left,这是为了让它们并行显示。

  我做的第二件事就是动画,因为我的布局不是position,所以我是用margin-left来实现位移的,margin-left:-图片宽度就能让第二张图片移动到carousel中。动画是使用jquery的animate函数。有关animate函数的使用详细的我就不说了,我只说我有用到的,animate(style,time,callback),这里的style是指最终样式,time是动画时长,callback是执行动画完后的回调函数。我只要对carousel-content做动画就可以了。

  最后一件事就是开始设计js插件了。之前我们插件的使用方式已经有了,就是$(DOM).carousel(config)。所以我的代码如下所示

(function (w,$) {
    $.fn.carousel = function (config) {
        config = config || {};
        var _config = {
            circle: config.circle || true, // 是否循环
            auto: config.auto || true,    // 是否自动轮播
            speed: config.speed || 1000,    // 轮播动画速度
            interval: 1500,    // 轮播间隔
        },
        _imgUrls = config.imgUrls || [],    // 图片地址
        _index = 0,    // 当前图片
        _carouselContent = null,    // 生成的DOM节点
        _carouselImgs = [],    // 生成的图片DOM节点
        _carouselIcons = [],
        _element = this[0],
        _intervalId = null;

        _config.interval += _config.speed;
        // 初始化
        var _init = function () {
            var carousel = document.createElement("div");
            carousel.className = "carousel";

            // 设置图片hover事件,停止动画
            carousel.onmouseenter = function () {
                if (_intervalId) {
                    clearInterval(_intervalId)
                }
            }
            // 设置鼠标离开事件,开始动画
            carousel.onmouseleave = function () {
                _setAnimate();
            }

            _element.appendChild(carousel);
            var carouselContent = document.createElement("div"),
                img,
                icon;

            // 创建img
            for (var i = 0, len = _imgUrls.length; i < len; i++) {
                img = document.createElement("img");
                img.src = _imgUrls[i];
                img.className = "carousel-img";
                img.width = carousel.offsetWidth;
                img.height = carousel.offsetHeight;

                carouselContent.appendChild(img);
                _carouselImgs.push(img);
            }

            // 创建圆形按钮
            var icons = document.createElement("div");
            for (var i = 0, len = _imgUrls.length; i < len; i++) {
                icon = document.createElement("div");
                icon.className = "carousel-icon";
                icons.appendChild(icon);
                if (i == 0) {
                    icon.style.marginLeft = 0;
                    icon.style.backgroundColor = "#E3E3E3";
                }
                (function (i) {
                    icon.onclick = function () {
                        _index = i;
                        _animateAction();
                    }
                })(i);
                _carouselIcons.push(icon);
            }
            carousel.appendChild(icons);
            icons.style.position = "absolute";
            icons.style.bottom = "30px";
            icons.style.left = "50%";
            icons.style.marginLeft = -icons.offsetWidth/2+"px";

            // 创建上一张和下一张按钮
            var lt = document.createElement("span"),
                gt = document.createElement("span");

            lt.className = "carousel-lt";
            gt.className = "carousel-gt";

            lt.innerText = "<";
            gt.innerText = ">";

            lt.style.lineHeight = carousel.offsetHeight + "px";
            gt.style.lineHeight = carousel.offsetHeight + "px";

            lt.onclick = function () {
                if (_index == 0) {
                    _index = _imgUrls.length - 1;
                } else {
                    _index--;
                }
                _animateAction();
            }

            gt.onclick = function () {
                if (_index == _imgUrls.length - 1) {
                    _index = 0;
                } else {
                    _index++;
                }
                _animateAction();
            }

            carousel.appendChild(lt);
            carousel.appendChild(gt);

            carouselContent.style.width = carousel.offsetWidth * len + "px";
            carousel.appendChild(carouselContent);
            _carouselContent = carouselContent;
            _setAnimate();
            // 窗口变化时的适配
            w.onresize = function () {
                $(_carouselContent).stop();
                _carouselContent.style.marginLeft = -(carousel.offsetWidth) * _index + "px"
                _carouselContent.style.width = carousel.offsetWidth * _carouselImgs.length + "px";
                for (var i = 0, len = _carouselImgs.length; i < len; i++) {
                    _carouselImgs[i].width = carousel.offsetWidth;
                    _carouselImgs[i].height = carousel.offsetHeight;
                }
            }
        },
        // 设置动画
        _setAnimate = function () {
            // 自动播放
            if (_config.auto) {
                _intervalId = setInterval(function () {
                    // 最后一张图片
                    if (_index == _imgUrls.length - 1) {
                        // 是否循环
                        if (_config.circle) {
                            // 回到第一张
                            _index = 0;
                            for (var i = 0, len = _carouselImgs.length; i < len; i++) {
                                _carouselImgs[i].style.marginLeft = 0;
                            }
                        } else {
                            clearInterval(_intervalId);
                        }
                    } else {
                        _index++;
                    }
                    // 开始动画
                    _animateAction();
                }, _config.interval)
            }
        },
        // 开始动画
        _animateAction = function () {
            for (var i = 0, len = _carouselIcons.length; i < len; i++) {
                _carouselIcons[i].style.backgroundColor = "#000";
            }
            _carouselIcons[_index].style.backgroundColor = "#E3E3E3";
            $(_carouselContent).animate({
                marginLeft: -(_element.offsetWidth) * _index + "px"
            }, _config.speed);
        }

        _init();
    }
}) (window,jQuery)

  上面最主要的代码是_init,_setAnimate,_animateAction这三个函数,_init函数是把生成DOM节点树,然后插入用户指定的地方。_setAnimate是设置动画,其实就是改变_index这个值。这个值是表示当前是到第几张图片,0表示第一张。_animateAction是设置动画,根据_index值使用animate设置动画。

  好了,这只是很简单的轮播代码,下面是html代码

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <style type="text/css">
        .carousel {
            width: 100%;
            height: 100%;
            overflow: hidden;
            position: relative;
        }
        .carousel-img {
            float: left;
        }
        .carousel-lt {
            position: absolute;
            top: 0;
            left: 0;
            width: 30px;
            height: 100%;
            text-align: center;
            font-size: 30px;
            color: #E3E3E3;
            cursor: pointer;
        }
        .carousel-lt:hover {
            background: #000;
        }
        .carousel-gt {
            position: absolute;
            top: 0;
            right: 0;
            width: 30px;
            height: 100%;
            text-align: center;
            font-size: 30px;
            color: #E3E3E3;
            cursor: pointer;
        }
        .carousel-gt:hover {
            background: #000;
        }
        .carousel-icon {
            width: 20px;
            height: 20px;
            background-color: #000;
            display: inline-block;
            vertical-align: middle;
            border-radius: 50%;
            margin-left: 10px;
            cursor: pointer;
        }
        .active {
            background-color: #E3E3E3;
        }
    </style>
</head>
<body>
    <div id="content" style="width: 100%; height: 500px">
    </div>
    <script type="text/javascript" src="./jquery.min.js"></script>
    <script type="text/javascript" src="./carousel.js"></script>
    <script type="text/javascript">
        $(document.getElementById("content")).carousel({
            imgUrls: [‘./2.jpg‘,‘./3.jpg‘,‘./4.jpg‘,‘./5.jpg‘]
        });
    </script>
</body>
</html>
时间: 2024-08-07 02:03:35

前端自制Jquery插件————轮播的相关文章

自己定义jquery插件轮播图

轮播图-html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible&

12款经典的白富美型—jquery图片轮播插件—前端开发必备

图片轮播是网站中的常用功能,用于在有限的网页空间内展示一组产品图片或者照片,同时还有非常吸引人的动画效果.本文向大家推荐12款实用的 jQuery 图片轮播效果插件,帮助你在你的项目中加入一些效果精美的图片轮播效果,希望这些插件能够帮助到你.Nivo Slider 首先推荐的这款插件号称世界上最棒的图片轮播插件,有独立的 jQuery 插件和 WordPress 插件两个版本. 目前下载量已经突破 1,800,000 次!jQuery 独立版本的插件主要有如下特色: ?  16个独特的过渡效果?

Nivo Slider - 世界上最棒的 jQuery 图片轮播插件

Nivo Slider 号称世界上最棒的图片轮播插件,有独立的 jQuery 插件和 WordPress 插件两个版本.目前下载量已经突破 1,800,000 次!jQuery 独立版本的插件主要有如下特色: ?  16个独特的过渡效果 ?  简洁和有效的标记 ?  加载参数设置 ?  内置方向和导航控制 ?  压缩版本大小只有12KB ?  支持链接图像 ?  支持 HTML 标题 ?  3套精美光滑的主题 ?  在MIT许可下免费使用 ?  支持响应式设计 插件下载     效果演示 您可能

jquery实现轮播插件

这几天用jquery写了两个轮播的插件,功能很简单.第一次尝试写插件,有很多不足的地方,代码如下: 注:图片链接请替换掉,配置信息必须加上图片width和height. <!DOCTYPE html> <html ng-app="myApp"> <head lang="en"> <meta charset="UTF-8"> <link rel="stylesheet" hr

10个免费的响应式jQuery Carousel 轮播图插件

jQuery Carousel 轮播图插件可以给网站创建华丽的动画效果,这里列出的10个jQuery Carousel 轮播图插件都是响应式的,并且可以免费使用. 1. ItemSlide.js 简单的触摸式Carousel 轮播图插件,提供多种样式的旋转动画,如向上滑动,基于全屏的触摸滑动,左右滑动等. 2. Liquid 每当Liquid 容器的大小改变时,都会重新调整列表中的项目数,以适应新的宽度. 3. jCarousel jCarousel 是一个用来控制水平或垂直方向上列表项的jQu

jQuery实现轮播效果(二) - 插件实现

开篇 前一篇文章(jQuery实现轮播效果(一) - 基础)讲到了怎样用jquery来实现一个简单的jquery轮播效果,基本的功能已经实现了,效果看起来还令人满意,唯一不足的就是:每次需要轮播效果时,要将代码复制粘贴过去,如果有些部分需要修改(例如:轮播时的动画效果,前一篇使用的是jquery的淡入淡出效果,如果改成滑动效果,不可避免的要修改js代码),那么就需要对js代码进行修改,对我所写的jquery轮播效果的js代码不熟悉的程序员来说,修改这些js确实很困难.轮播插件所要实现的目标之一就

jQuery图片轮播(二)利用构造函数和原型创建对象以实现继承

上一篇文中完成的封装,还存在一个小问题,就是该轮播对象不能在同一页面中重复使用,本文将通过组合使用javascript的构造函数和原型模式创建对象来解决这个问题. 没有看过上一篇文章的朋友可以点此查看上一篇文章 (jQuery图片轮播(一)轮播实现并封装) 首先回顾一下,上文的问题所在,上文中的carsouel对象是采用字面量的方式来定义的,这样carsouel本就是一个实例,想要使用在多处时,这个对象的方法会发生冲突,最终只会执行最后的那一个.而通过采用构造函数的方式来定义对象carsouel

jQuery实现轮播图

我用的jQuery库jquery-3.2.1.js可以在官网下载:http://jquery.com/download/ <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <

84JS插件:jQuery版轮播图

一.代码分两个部分:1.HTML部分,根据注释处理即可:2.基于jQuery的play.js插件部分,这里为了展示效果,直接写在<html></html>下的<script></script>标签里.3.效果包含:自动轮播,焦点对齐,前进后退,直走不返,鼠标进入.轮播停止且前进后退图标出现,鼠标离开.轮播重启且前进后退图标隐藏.4.这里可以预览效果.二.轮播图原理说明:(1)轮播图(假设)有7张图片,“一”字排列,把第1张图片复制一次,放到第8张的位置,这样