Home > Web Front-end > JS Tutorial > jquery to create multifunctional carousel image plug-in_jquery

jquery to create multifunctional carousel image plug-in_jquery

WBOY
Release: 2016-05-16 16:06:26
Original
1217 people have browsed it

This is a multi-functional slideshow plug-in that supports multiple configurations. The css style is found online, and the code inside is original, light spray~

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

;(function($) {

  "use strict";

  var methods = {

    o : {

      next: "#cycle-next",

      prev: "#cycle-prev",

      pager : "#cycle-nav",

      slider : "#beauty-slider",

      timeLine : "#timeLine",

      innerTimeLine : "#inner-timeLine",

      timeLineNode : "#timeLine-node",

      sliderItemClass : "sliderItem",

      nodeActive : "node_active",

      displays : {fade : "fade",left : "left",right : "right",top : "top",bottom : "bottom"},

      navHtml : "<a href='javascript:;' class='&'>&#63;</a>",

      navConfig : {navBoxClass : "cycle-nav", navActiveClass: "activeSlide" ,showNum : true}

    },

    generateId : function(){

      return "-" + new Date().getTime();

    },

    generateTemplate : function(id,settings){

      var htmls = new Array();

      htmls.push('<div class="slider-wrap">');

      htmls.push('<div class="cycleslider-wrap">');

      htmls.push('<div id="beauty-slider' + id + '" class="cycleslider"></div>');

      if(settings.isArrow) {

        htmls.push('<a id="cycle-prev' + id + '" class="cycle-prev" href="javascript:;" title="试试左方向键翻页">Prev</a>');

        htmls.push('<a id="cycle-next' + id + '" class="cycle-next" href="javascript:;" title="试试右方向键翻页">Next</a>');

      }

      if(settings.showTimeLine) {

        htmls.push('<div class="timeLine" id="timeLine' + id + '">');

        htmls.push('<div class="innerTimeLine" id="inner-timeLine' + id + '" style="width:0px;"> </div>');

        htmls.push('<div id="timeLine-node' + id + '"></div>');

        htmls.push('</div>');

      }

      if(settings.isNav) {

        htmls.push('<div id="cycle-nav' + id + '" class="cycle-nav"></div>');

      }

      htmls.push('</div>');

      htmls.push('<div class="loader"></div>');

      htmls.push('</div>');

      return htmls;

    },

    init : function(dom, options) {

      var s = this;

      var defaults = { url : "", data : {} , auto : false ,time : 2000, overLay : false, isArrow : true, isNav : true ,showTimeLine : false,showTip : false, keyboard : true, display : s.o.displays.fade,navConfig : s.o.navConfig};

      var settings = $.extend({},defaults, options);

      var id = s.generateId();

      $(".slider-wrap .loader").show();

      var imgArray = s.returnImgArray(dom, settings);

      if (imgArray != null && imgArray.length > 0) {

        s["imgcnt" + id] = imgArray.length;

        $(dom).html(s.generateTemplate(id,settings).join('')).show();

        var slider = $(s.o.slider + id);

        var pager = $(s.o.pager + id);

        var timeLineNode = $(s.o.timeLineNode + id);

        s.o.innerW = $(dom).width() - 20;

        var imgHtml = "";

        var pageHtml = "";

        var timeHtml = "";

        $.each(imgArray,function(index,item){

          var picClass = index == 0 &#63; s.o.sliderItemClass : s.o.sliderItemClass + " none";

          var navClass = index == 0 &#63; settings.navConfig.navActiveClass : "";

          imgHtml +='<div class="' + picClass + '"><a href="'+ ( s.isParamValid(item.picUrl) &#63; item.picUrl : "javascript:;" ) +'"><img src="' + item.picPath + '" width="'+item.width+'" height="' +item.height+ '" title="' + item.title + '"/></a></div>';

          pageHtml += s.o.navHtml.replace("&#63;", settings.navConfig.showNum &#63; index + 1 : "").replace("&", navClass);

          var left = s.o.innerW * index / s["imgcnt" + id] - 13;

          var nodeClass = index == 0 &#63; s.o.nodeActive : "";

          timeHtml +='<div class="node ' + nodeClass + '" style="left: ' + left + 'px;">';

          if(settings.showTip) {

            timeHtml +='<div class="tooltip">' + item.title + '</div>';

          }

          timeHtml +='</div>';

        });

        slider.html(imgHtml);

        if(settings.isNav) {

          if(settings.navConfig.navBoxClass) {

            pager.removeClass(s.o.navConfig.navBoxClass).addClass(settings.navConfig.navBoxClass);

          }

          pager.html(pageHtml);

        }

        if(settings.showTimeLine) {

          timeLineNode.html(timeHtml);

        }

        slider.width(s.o.innerW);

        slider.find("." + s.o.sliderItemClass).width(s.o.innerW);

        $(s.o.timeLine + id).width(s.o.innerW);

        if(settings.overLay) {

          if($.fn.layerModel) {

            $(dom).layerModel({staySame : true, blurClose : true});

          } else {

            alert("请先引入layerModel插件!");

          }

        }

        s.initBind(id, settings);

        s["currentIndex" + id] = 0;

        var totalTime = settings.time / 1000 * s["imgcnt" + id];

        if(settings.auto) {

          if(settings.showTimeLine) {

            s.startTimeLine(id , totalTime,settings);

          } else {

            s.o.timeInterval = window.setInterval(function(){

              $("#cycle-next" + id).click();

            }, settings.time);

          }

        }

      } else {

        return;

      }

      return dom;

    },

    startTimeLine : function(id ,time, settings) {

      var s = this;

      var $innerTimeLine = $(s.o.innerTimeLine + id);

      var crnW = $innerTimeLine.width();

      for (var i = 0; i < s["imgcnt" + id]; i++) {

        var autoW = Math.floor(s.o.innerW * i / s["imgcnt" + id]);

        if(crnW == autoW) {

          s["currentIndex" + id] = i;

          s.updateImgLink(id, settings);

          $(s.o.timeLineNode + id).find("div.node").eq(i).addClass(s.o.nodeActive).siblings().removeClass(s.o.nodeActive);

        }

      }

      if(crnW < s.o.innerW) {

        $innerTimeLine.animate({width : "+=1px"}, time , 'linear', function(){

          s.startTimeLine(id, time, settings);

        });

      } else {

        $innerTimeLine.width(0);

        s.startTimeLine(id, time, settings);

      }

    },

    initBind :function(id, settings) {

      var s = this;

      var isEasing = $.easing.def;

      if(settings.isArrow) {

        $("#cycle-prev" + id + ", #cycle-next" + id).css({opacity: "0"});

        $(".cycleslider-wrap").hover(function(){

          $("#cycle-prev" + id).stop().animate({left: "-31", opacity: "1"},200,isEasing &#63; "easeOutCubic" : "");

          $("#cycle-next" + id).stop().animate({right: "-31", opacity: "1"},200,isEasing &#63;"easeOutCubic" : ""); 

        }, function() {

          $("#cycle-prev" + id).stop().animate({left: "-50", opacity: "0"},400,isEasing &#63;"easeInCubic" : "");

          $("#cycle-next" + id).stop().animate({right: "-50", opacity: "0"},400,isEasing &#63;"easeInCubic" : "");   

        });

        $("#cycle-prev" + id).bind("click",function(){

          s["currentIndex" + id] = s["currentIndex" + id] <= 0 &#63; s["imgcnt" + id] - 1 : s["currentIndex" + id] - 1;

          s.updateImgLink(id,settings);

        });

        $("#cycle-next" + id).bind("click",function(){

          s["currentIndex" + id] = s["currentIndex" + id] < s["imgcnt" + id] - 1 &#63; s["currentIndex" + id] + 1 : 0;

          s.updateImgLink(id,settings);

        });

      }

      $(".slider-wrap .loader").hide();

      $("a",s.o.pager + id).bind("click",function(){

        if ($(this).hasClass(s.o.navConfig.navActiveClass)){

          return false;

        }

        s["currentIndex" + id] = $(this).index();

        s.updateImgLink(id,settings);

      });

        

      if(settings.auto && settings.showTimeLine) {

        $("div.node",s.o.timeLineNode + id).bind({

          click : function(){

            if($(this).hasClass(s.o.nodeActive)){

              return false;

            }

            s["currentIndex" + id] = $(this).index();

            s.updateImgLink(id,settings);

          },

          mouseover : function(){

            if(settings.showTip) {

              $(this).find(".tooltip").fadeIn();

            }

          },

          mouseout : function(){

            if(settings.showTip) {

              $(this).find(".tooltip").fadeOut();

            }

          }

        });

      }

      // 键盘操作

      if(settings.keyboard){

        $(window).keydown(function(event){

          //<---

          if(event.keyCode == 37){

            $("#cycle-prev" + id).click();

          }

          //--->

          if(event.keyCode == 39){

            $("#cycle-next" + id).click();

          }

        });

      }

    },

    updateImgLink : function(id, settings){

      var s = this;

      var index = s["currentIndex" + id];

      var display = settings.display;

      var isEasing = $.easing.def;

      var $items = $("div." + s.o.sliderItemClass,s.o.slider + id);

      switch (display) {

        case s.o.displays.fade :

          $items.eq(index).show().siblings().hide();

          break;

        case s.o.displays.left :

          $items.css({ position : "absolute", top : 0, left : s.o.innerW }).hide();

          $items.eq(index).animate({ left : 0 }, 100, isEasing &#63; "easeOutCubic" : "").show();

          $(s.o.slider + id).height(s.getImgMaxHeight(id));

          break;

        case s.o.displays.right :

          $items.css({ position : "absolute", top : 0, right : s.o.innerW }).hide();

          $items.eq(index).animate({ right : 0 }, 100, isEasing &#63; "easeOutCubic" : "").show();

          $(s.o.slider + id).height(s.getImgMaxHeight(id));

          break;

        case s.o.displays.top :

          break;

        case s.o.displays.bottom :

          break;

        default :

          break;

      }

      $("a",s.o.pager + id).eq(index).addClass(settings.navConfig.navActiveClass).siblings().removeClass(settings.navConfig.navActiveClass);

      // 更新进度条的信息

      if(settings.auto && settings.showTimeLine) {

        var indexWidth = Math.floor(s.o.innerW * index / s["imgcnt" + id]);

        $(s.o.innerTimeLine + id).animate({width : indexWidth +"px"}, 500 , 'linear', function(){

          $(s.o.timeLineNode + id).find("div.node").eq(index).addClass(s.o.nodeActive).siblings().removeClass(s.o.nodeActive);

        });

      }

    },

    getImgMaxHeight : function(id) {

      var s = this;

      var slider = $(s.o.slider + id);

      var maxHeight = 0;

      $.each(slider.find("img"),function(index,item){

        var imgHeight = this.height;

        maxHeight = Math.max(maxHeight,imgHeight);

      });

      return maxHeight;

    },

    returnImgArray : function(dom,settings){

      var s = this;

      var imgArray = new Array();

      var url = settings.url;

      if (s.isParamValid(url)) {

        $.ajax({

          url : url + "&#63;t=" + new Date().getTime(),

          type : 'POST',

          async : false,

          data : settings.data,

          dataType : 'json',

          success : function(data) {

            if (s.isParamValid(data)) {

              $.each(data, function(index, item) {

                imgArray.push(item);

              });

            }

          },

          error : function() {

            alert("图片数据源地址无效...");

            return null;

          },

          complete : function(XHR, TS) { XHR = null; }

        });

      } else {

        var imgs = $(dom).find("img");

        if (imgs != null && imgs.length > 0) {

          $.each(imgs,function(i,o){

            var pic = new Object();

            pic.picUrl = $(o).parent("a").attr("href");

            pic.picPath = $(o).attr("src");

            pic.width = $(o).attr("width");

            pic.height = $(o).attr("height");

            pic.title = $(o).attr("title");

            imgArray.push(pic);

          });

        }

      }

      return imgArray;

    },

    isParamValid : function(v) {

      return !(v == "" || v == null || v == undefined);

    }

  };

  $.fn.beautyFocus = function(options) {

    return this.each(function(index,item) {

      methods.init(item, options); 

    });

  };

})(jQuery);

Copy after login

The above is the entire content of this article, I hope you all like it.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template