轮播图插件需要思考那些不确定因 jq轮播图插件 - 图1代码如下

    1. (function ($) {
    2. function Swiper(option, self) {
    3. // dom 元素
    4. this.list = option.list || [];
    5. // 宽度
    6. this.width = option.width || $(self).width();
    7. // 高度
    8. this.height = option.height || $(self).height();
    9. // 轮播方式 type: 'animation'(从左到右的轮播) 'fade' (淡入淡出的轮播)
    10. this.type = option.type || 'fade';
    11. // 是否自动轮播
    12. this.autoChange = typeof option.autoChange === 'undefined' ? true : option.autoChange;
    13. // 轮播时间
    14. this.autoTime = option.autoTime || 5000;
    15. // 是否展示左右按钮 showChangeBtn: 'always', 'hover', 'hidden'
    16. this.showChangeBtn = option.showChangeBtn || 'always';
    17. // 是否展示小圆点
    18. this.showSpotBtn = typeof option.showSpotBtn === 'undefined' ? true : option.showSpotBtn;
    19. // 小圆点的位置 spotPosition: 'left', 'right', 'center'
    20. this.spotPosition = option.spotPosition || 'left';
    21. // 小圆点的颜色
    22. this.spotColor = option.spotColor || 'red';
    23. // 校园度的尺寸
    24. this.sportSize = option.sportSize || 10;
    25. this.listLen = this.list.length;
    26. this.warp = self;
    27. this.nowIndex = 0;
    28. this.timer = null;
    29. this.flag = true;
    30. }
    31. Swiper.prototype.init = function () {
    32. this.createDom()
    33. this.initStyle()
    34. this.bindEvent()
    35. this.autoMove()
    36. }
    37. /**
    38. * 创建html结构
    39. */
    40. Swiper.prototype.createDom = function () {
    41. // 创建轮播图包含块 轮播区域 小圆点
    42. var swiperWarp = $('<div class="my-swiper"></div>');
    43. var domList = $('<ul class="my-swiper-list"></ul>');
    44. var spotBtn = $('<div class="my-swiper-spots"></div>');
    45. // 创建li与小圆点
    46. var len = this.list.length;
    47. for (var i = 0; i < len; i++) {
    48. var oLi = $('<li class="my-swiper-item"></li>');
    49. oLi.append(this.list[i]).appendTo(domList);
    50. spotBtn.append($('<span></span>'));
    51. }
    52. // 判断是轮播方式是否为普通轮播,普通轮播需要多处一张图片辅助轮播
    53. if (this.type === 'animation') {
    54. domList.append($('<li class="my-swiper-item"></li>').append($(this.list[0]).clone(true)));
    55. }
    56. // 添加左右滚动按钮
    57. var swiperLeftBtn = $('<div class="my-swiper-btn my-swiper-lbtn">&lt;</div>');
    58. var swiperRightBtn = $('<div class="my-swiper-btn my-swiper-rbtn">&gt;</div>');
    59. // 将结构添加进页面
    60. swiperWarp.append(domList)
    61. .append(swiperLeftBtn)
    62. .append(swiperRightBtn)
    63. .append(spotBtn)
    64. .appendTo(this.warp)
    65. .addClass("my-swiper-" + this.type);
    66. }
    67. /**
    68. * 设置样式
    69. */
    70. Swiper.prototype.initStyle = function () {
    71. var self = this;
    72. // 设置轮播区域的宽度与高度 普通轮播区域的ul的宽的需要多加一个li的宽度
    73. $('.my-swiper', this.warp).css({
    74. width: this.width,
    75. height: this.height,
    76. }).find('.my-swiper-list').css({
    77. width: this.type === "animation" ? this.width * (this.listLen + 1) : this.width,
    78. }).find('.my-swiper-item').css({
    79. width: this.width,
    80. });
    81. // 设置淡入淡出轮播图样式
    82. if (this.type === 'fade') {
    83. $('.my-swiper > .my-swiper-list > .my-swiper-item', this.warp).css({
    84. display: 'none',
    85. }).eq(this.nowIndex).css({
    86. display: 'block',
    87. })
    88. }
    89. // 设置小圆点的样式
    90. $('.my-swiper-spots', this.warp).css({
    91. textAlign: this.spotPosition,
    92. display: this.showSpotBtn ? 'block' : 'none',
    93. }).find('span').eq(this.nowIndex).css({
    94. backgroundColor: this.spotColor,
    95. })
    96. // 小圆点的尺寸
    97. $('.my-swiper-spots > span' ,this.warp).css({
    98. width:this.sportSize,
    99. height:this.sportSize,
    100. })
    101. // 设置左右切换按钮的显示方式
    102. if (this.showChangeBtn === 'always') {
    103. $('.my-swiper > my-swiper-btn', this.warp).css({
    104. display: 'block',
    105. })
    106. } else if (this.showChangeBtn === 'hidden') {
    107. $('.my-swiper > .my-swiper-btn', this.warp).css({
    108. display: 'none',
    109. })
    110. } else if (this.showChangeBtn === 'hover') {
    111. $('.my-swiper > .my-swiper-btn', self.warp).css({
    112. display:'none',
    113. })
    114. $('.my-swiper', this.warp).hover(function () {
    115. $('.my-swiper > .my-swiper-btn', self.warp).css({
    116. display: 'block',
    117. })
    118. }, function () {
    119. $('.my-swiper > .my-swiper-btn', self.warp).css({
    120. display: 'none',
    121. })
    122. })
    123. }
    124. }
    125. /**
    126. * 设置事件
    127. */
    128. Swiper.prototype.bindEvent = function () {
    129. // 左切换图片
    130. var self = this;
    131. $('.my-swiper-lbtn', this.warp).click(function () {
    132. if (self.flag) {
    133. self.flag = false;
    134. } else {
    135. return false;
    136. }
    137. // 判断是否为第一张
    138. // clearInterval(this.timer)
    139. if (self.nowIndex === 0) {
    140. // 判断是否为普通轮播
    141. if (self.type === 'animation') {
    142. $('.my-swiper-list', self.warp).css({
    143. left: -self.listLen * self.width,
    144. })
    145. }
    146. // 如果为淡入淡出
    147. self.nowIndex = self.listLen - 1;
    148. } else {
    149. self.nowIndex--;
    150. }
    151. self.swiperMove()
    152. })
    153. // 右切换图片
    154. $('.my-swiper-rbtn', this.warp).click(function () {
    155. if (self.flag) {
    156. self.flag = false;
    157. } else {
    158. return false;
    159. }
    160. // 判断是否为普通轮播动画
    161. // clearInterval(this.timer)
    162. if (self.type === 'animation') {
    163. // 判断是否为最后一张图片
    164. if (self.nowIndex == self.listLen) {
    165. $('.my-swiper-list', self.warp).css({
    166. left: 0,
    167. })
    168. self.nowIndex = 1;
    169. } else {
    170. self.nowIndex++;
    171. }
    172. } else {
    173. // 淡入淡动画
    174. if (self.nowIndex === self.listLen - 1) {
    175. self.nowIndex = 0;
    176. } else {
    177. self.nowIndex++;
    178. }
    179. }
    180. self.swiperMove()
    181. })
    182. // 鼠标移入时停止,鼠标移出时是否自动轮播
    183. $('.my-swiper', this.warp).mouseenter(function () {
    184. clearInterval(self.timer);
    185. }).mouseleave(function () {
    186. if (self.autoChange) {
    187. self.autoMove()
    188. }
    189. })
    190. // 鼠标移入小圆点,播放其对应的图片
    191. $('.my-swiper-spots > span').mouseenter(function () {
    192. if (self.flag) {
    193. self.flag = false;
    194. } else {
    195. return false;
    196. }
    197. self.nowIndex = $(this).index()
    198. self.swiperMove()
    199. })
    200. }
    201. /***
    202. * 轮播动画
    203. */
    204. Swiper.prototype.swiperMove = function () {
    205. var self = this;
    206. // 判断是否为普通动画
    207. if (this.type === 'animation') {
    208. $('.my-swiper-list', self.warp).animate({
    209. left: -self.nowIndex * self.width,
    210. }, function () {
    211. self.flag = true;
    212. })
    213. } else {
    214. // 淡入淡出轮播动画
    215. $('.my-swiper-list > .my-swiper-item', this.warp)
    216. .fadeOut()
    217. .eq(this.nowIndex)
    218. .fadeIn(function () {
    219. self.flag = true;
    220. })
    221. }
    222. // 切换小圆点
    223. $('.my-swiper-spots > span', this.warp).css({
    224. backgroundColor: "#ccc",
    225. }).eq(this.nowIndex % this.listLen).css({
    226. backgroundColor: this.spotColor,
    227. })
    228. }
    229. // 自动轮播
    230. Swiper.prototype.autoMove = function () {
    231. var self = this;
    232. clearInterval(this.timer);
    233. if (this.autoChange) {
    234. this.timer = setInterval(function () {
    235. $('.my-swiper > .my-swiper-rbtn', self.warp).trigger('click');
    236. }, this.autoTime)
    237. }
    238. }
    239. $.fn.extend({
    240. swiper: function (option) {
    241. var obj = new Swiper(option, this);
    242. obj.init()
    243. }
    244. })
    245. }($ || jQuery))

    调用方式

    1. $('.swiper-1').swiper({
    2. list:$('.focus-item-img'), //需要轮播的图片
    3. width: 590,
    4. height: 470,
    5. type: 'fade',
    6. autoChange: true,
    7. autoTime: 3000,
    8. showChangeBtn: 'always',
    9. showSpotBtn: true,
    10. spotPosition: 'left',
    11. spotColor: 'red',
    12. })

    html结构为

    1. <div class="swiper-1">
    2. <img class="focus-item-img"
    3. src="http://imgcps.jd.com/ling4/100010617232/5Lqs6YCJ5aW96LSn/5L2g5YC85b6X5oul5pyJ/p-5bd8253082acdd181d02fa1b/b06140e4/cr/s/q.jpg">
    4. <img class="focus-item-img"
    5. src="http://img20.360buyimg.com/pop/s590x470_jfs/t1/116840/9/15280/74505/5f3cf320Eebd6a908/7b799f81542a23ec.jpg.webp">
    6. <img class="focus-item-img"
    7. src="http://img13.360buyimg.com/pop/s590x470_jfs/t1/135367/9/7234/100878/5f3ba809E1b202a8b/81b0be8389b4089a.jpg.webp">
    8. <img class="focus-item-img"
    9. src="http://img11.360buyimg.com/pop/s590x470_jfs/t1/118356/11/11706/83497/5f0298b3Ee6ef2cc4/c512647552ff81ef.jpg.webp">
    10. </div>