


Full record of jQuery focus map switching simple plug-in production process_jquery
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>
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}
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>
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); }; }));
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; }
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); } }
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
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; }
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); }); }
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);//暂停的时候就取消自动切换 } }
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

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
