Home > Web Front-end > JS Tutorial > Full record of jQuery focus map switching simple plug-in production process_jquery

Full record of jQuery focus map switching simple plug-in production process_jquery

WBOY
Release: 2016-05-16 16:38:32
Original
1286 people have browsed it

The homepage often needs a focus image switching effect, and the recent projects also require it, so I searched online and found a semi-finished plug-in. I modified it myself.

There are two folders jquery.jslide.js and jquery.jslides.js under the js folder. The first one was rewritten by me, and the second one is the original author's file. The picture below is the rendering:

1. Static effect

<div class="slide_wrap">
 <ul id="slides2" class="slide">
  <li style="background:url('images/01.jpg') no-repeat center top"><a href="http://www.jb51.net/" target="_blank">pwstrick1</a></li>
  <li style="background:url('images/02.jpg') no-repeat center top"><a href="http://www.jb51.net/" target="_blank">pwstrick2</a></li>
  <li style="background:url('images/03.jpg') no-repeat center top"><a href="http://www.jb51.net/" target="_blank">pwstrick3</a></li>
  <li style="background:url('images/04.jpg') no-repeat center top"><a href="http://www.jb51.net/" target="_blank">pwstrick4</a></li>
 </ul>
</div>
Copy after login
1. Widescreen focus image switching is now more popular. In the past, labels used img to display images, but now they have to be replaced with background as the background image, so that horizontal scroll bars will not appear.

2. Set a slide_wrap on the outermost surface to limit the absolute positioning of the images inside

I originally added the class in 3.ul when the plug-in was initialized. Now I have added it in advance. The display effect is better than adding it later. You can make modifications when rewriting the plug-in

.slide_wrap {width:100%;height:400px;position:relative}
.slide_wrap .slide { display:block; width:100%; height:400px; list-style:none; padding:0; margin:0; position:relative;}
.slide_wrap .slide li { display:block; width:100%; height:100%; list-style:none; padding:0; margin:0; position:absolute;}
.slide_wrap .slide li a { display:block; width:100%; height:100%; text-indent:-9999px;}
.slide_wrap .pagination { display:block; list-style:none; position:absolute; left:50%; top:340px; z-index:9900;padding:5px 15px 5px 0; margin:0}
.slide_wrap .pagination li { display:block; list-style:none; width:10px; height:10px; float:left;margin-left:15px; border-radius:5px; background:#FFF }
.slide_wrap .pagination li a { display:block; width:100%; height:100%; padding:0; margin:0; text-indent:-9999px;outline:0}
.slide_wrap .pagination li.current { background:#0092CE}
Copy after login

1. The height attribute in slide_wrap and slide can be modified according to the actual situation

2. Pagination is the button style in the picture. It is used to control which picture is displayed. It is also an absolute positioning left and top that can be modified according to the actual situation

3. Each color in the style can also be customized according to the desired effect

4. The above style is a bit verbose. It can be simplified appropriately when embedded in your own project

2. Calling method

<script type="text/javascript">
 $('#slides2').jslide();
</script>
Copy after login

1. Set ul as a focus image plug-in

2. Each of the following operations will revolve around ul

3. Common format of jQuery plug-in

;(function (factory) {
 'use strict';
 // Register as an AMD module, compatible with script loaders like RequireJS.
 if (typeof define === 'function' && define.amd) {
  define(['jquery'], factory);
 }
 else {
  factory(jQuery);
 }
}(function ($, undefined) {
 'use strict';

   //中间插件代码

 $.fn.jslide = function (method) {
  return _init.apply(this, arguments);
 };
}));
Copy after login

1. The first semicolon is to prevent syntax errors from being combined with other codes on one line when compressed together. For example, var i=0(function(factory){......}(..);

2. 'use strict' turns on strict mode, allowing the Javascript interpreter to use "strict" syntax to parse code to help developers find errors

3. If the requirejs module is used to load the framework, define(['jquery'], factory) is to make the plug-in support the AMD specification

4. function ($, undefined) The undefined here is to prevent the use of rewritten undefined when introducing other js files

5. _init is used for initialization effect

4. Plug-in initialization

  var defaults = {
  speed : 3000,
  pageCss : 'pagination',
  auto: true //自动切换
 };
 
 var nowImage = 0;//现在是哪张图片
 var pause = false;//暂停
 var autoMethod;
  /**
  * @method private
  * @name _init
  * @description Initializes plugin
  * @param opts [object] "Initialization options"
  */
 function _init(opts) {
  opts = $.extend({}, defaults, opts || {});
  // Apply to each element
  var $items = $(this);
  for (var i = 0, count = $items.length; i < count; i++) {
   _build($items.eq(i), opts);
  }
  return $items;
 }
Copy after login

1. Defaults are exposed custom parameters. Here I have written three automatic switching speeds, selection button styles, and whether to automate

2. Three global parameters, nowImage is the serial number of the currently displayed image, pause controls whether the image is switched or paused, and autoMethod is the number of the timing function

3. Custom parameters are merged in _init and _build is called to create the operation

5. Various operations of creating a plug-in

  /**
  * @method private
  * @name _getSlides
  * @description 获取幻灯片对象
  * @param $node [jQuery object] "目标对象"
  */
 function _getSlides($node) {
  return $node.children('li');
 }
  /**
  * @method private
  * @name _build
  * @description Builds each instance
  * @param $node [jQuery object] "目标对象"
  * @param opts [object] "插件参数"
  */
 function _build($node, opts) {
  var $slides = _getSlides($node);
  $slides.eq(0).siblings('li').css({'display':'none'});
  var numpic = $slides.size() - 1;
  
  $node.delegate('li', 'mouseenter', function() {
   pause = true;//暂停轮播
   clearInterval(autoMethod);
  }).delegate('li', 'mouseleave', function() {
   pause = false;
   autoMethod = setInterval(function() {
    _auto($slides, $pages, opts);
   }, opts.speed);
  });
  //console.log(autoMethod)
  var $pages = _pagination($node, opts, numpic);
  
  if(opts.auto) {
   autoMethod = setInterval(function() {
    _auto($slides, $pages, opts);
   }, opts.speed);
  }
 } 
Copy after login

1. _getSlides is used to obtain the li sub-tag of the ul object, which is the focus map plug-in

2. Set other tags except the first li tag to be hidden

3. Get the number of switched pictures. Since the subsequent loop starts from subscript 0 and does the <= operation, a 1 is subtracted. In fact, it is okay not to subtract here. It depends on personal preference

4. Set the mouseenter and mouseleave events for the li tag, which are to cancel the loop and continue the loop respectively

5. Initialization selection button

6. If the parameter auto is true, automatic switching will be activated

6. Initialization selection button

   /**
  * @method private
  * @name _pagination
  * @description 初始化选择按钮
  * @param $node [jQuery object] "目标对象"
  * @param opts [Object] "参数"
  * @param size [int] "图片数量"
  */
  function _pagination($node, opts, size) {
  var $ul = $('<ul>', {'class': opts.pageCss});
  for(var i = 0; i <= size; i++){
   $ul.append('<li>' + '<a href="javascript:void(0)">' + (i+1) + '</a>' + '</li>');
  }
  
  $ul.children(':first').addClass('current');//给第一个按钮选中样式
  var $pages = $ul.children('li');
  $ul.delegate('li', 'click', function() {//绑定click事件
   var changenow = $(this).index();
   _changePage($pages, $node, changenow);
  }).delegate('li', 'mouseenter', function() {
   pause = true;//暂停轮播
  }).delegate('li', 'mouseleave', function() {
   pause = false;
  });
  $node.after($ul);
  return $pages;
  }
Copy after login

1. Dynamically add the button ul tag, assign it a custom class, and add the sub-tag li

2. Add a selection style to the first button

3. Add click, mouseenter and mouseleave events to the li tag, and bind the click event to the switching operation

4. Place the paging button behind the plug-in object ul

5. Return to the li object in the paging button, which will be useful later

7. Switch pictures

   /**
  * @method private
  * @name _change
  * @description 幻灯片显示与影藏
  * @param $slides [jQuery object] "图片对象"
  * @param $pages [jQuery object] "按钮对象"
  * @param next [int] "要显示的下一个序号"
  */
  function _fadeinout($slides, $pages, next){
  $slides.eq(nowImage).css('z-index','2');
  $slides.eq(next).css({'z-index':'1'}).show();
  $pages.eq(next).addClass('current').siblings().removeClass('current');
  $slides.eq(nowImage).fadeOut(400, function(){
   $slides.eq(next).fadeIn(500);
  });
 }
Copy after login

1. Increase the z-index of the current picture, increase the z-index of the next picture, and display the next picture. This can create a gradient effect. Otherwise, it will be very confusing. A blunt switch

2. Add a selection style to the next selection button

3. Use jQuery’s fadeOut and fadeIn to create hidden and displayed gradient effects

8. Automatic cycle

   /**
  * @method private
  * @name _auto
  * @description 自动轮播
  * @param $slides [jQuery object] "图片对象"
  * @param $pages [jQuery object] "按钮对象"
  * @param opts [Object] "参数"
  */
  function _auto($slides, $pages, opts){
  var next = nowImage + 1;
  var size = $slides.size() - 1;
  if(!pause) {
   if(nowImage >= size){
    next = 0;
   }
   
   _fadeinout($slides, $pages, next);
   
   if(nowImage < size){
    nowImage += 1;
   }else {
    nowImage = 0;
   }
  }else {
   clearInterval(autoMethod);//暂停的时候就取消自动切换
  }
  }

Copy after login

1. Determine whether to pause or continue the carousel

2. If it is not paused, set the serial numbers of the current page and the next button according to the conditions

There are still many problems with the plug-in, such as the inability to bind two different objects on one page, and there is huge room for modification.

Through this modification, I have a controllable focus map switching plug-in. Although there are still many problems, they can be solved step by step. It will be much more convenient to embed it into your own project in the future.

demo:http://demo.jb51.net/js/2014/jsilde/

Download: http://www.jb51.net/jiaoben/210405.html

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