


The jquery plug-in jquery.LightBox.js realizes the effect of clicking to enlarge the picture and clicking left and right (with demo source code download)_jquery
May 16, 2016 pm 03:13 PMThe example in this article describes the jquery plug-in jquery.LightBox.js to achieve the effect of clicking to enlarge the picture and clicking left and right to switch. Share it with everyone for your reference, the details are as follows:
This plug-in is written by the author of the article. The purpose is to improve the author's js ability and also provide some convenience for some js novices when using the plug-in. Veterans can just fly by leisurely.
This plug-in is designed to achieve the currently popular effects of clicking to enlarge a picture and clicking left and right to switch pictures. You can set whether to add the effect of switching pictures left or right according to your actual needs. The overall code is as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標題文檔</title> <style> *{margin:0;padding:0;} li{list-style:none;} .item{margin:20px;} .item ul li{float:left;margin-right:20px;} .item ul li img{width:100px;cursor:pointer;} .lb_wrap{display:none;} .lightbox_bg{background:#000;filter:alpha(opacity=70);opacity:.7;position:absolute;left:0;top:0;width:100%;height:100%;z-index:1;} .lightbox{position:absolute;left:0;top:50%;width:100%;z-index:2;text-align:center;} .lightbox p{position:absolute;height:61px;width:38px;top:50%;left:0;z-index:2;background:transparent url(themes.png) no-repeat left top;margin-top:-30.5px;cursor:pointer;} .lightbox p.next{left:auto;background-position:right top;right:0;} </style> <script type="text/javascript" src="jquery-1.7.1.js"></script> <script type="text/javascript" src="jquery.LightBox.js"></script> </head> <body> <div class="item"> <ul> <li><img src="01.jpg" /></li> <li><img src="02.jpg" /></li> <li><img src="03.jpg" /></li> <li><img src="04.jpg" /></li> <li><img src="05.jpg" /></li> <li><img src="06.jpg" /></li> </ul> </div> <script> $(function(){ $(".item").LightBox({ controls : true //上一張、下一張按鈕是否顯示,默認是顯示true }); }) </script> </body> </html>
Plug-in jquery.LightBox.js code:
/* *LightBox 1.0 *dependence jquery-1.7.1.js */ ;(function(a){ a.fn.LightBox = function(options){ var defaults = { controls : true //上一張、下一張按鈕是否顯示,默認是顯示true } var opts = a.extend(defaults, options); var lb_wrap = '<div class="lb_wrap"><div class="lightbox_bg"></div><div class="lightbox"><img src="loading.gif" class="lg_img"></div></div>'; a("body").append(lb_wrap); //controls if(opts.controls){ a(".lightbox").append('<p class="prev"></p><p class="next"></p>'); } function imgobj(obj1, obj2){ //imgObj.height是通過img對象獲取的圖片的實際高度 var imgObj = new Image(); imgObj.src = obj1.attr("src"); var margintop = 0 - (imgObj.height)/2; obj2.css("margin-top",margintop); } this.each(function(){ var obj = a(this); var numpic = obj.find("li").length; var num = 0; //點擊賦值并顯示 obj.find("img").click(function(){ var src = a(this).attr("src"); a(".lg_img").attr("src",src); imgobj(a(".lg_img"), a(".lightbox")); a(".lb_wrap").fadeIn(); a(".lg_img").fadeIn(); a(".prev").fadeIn().siblings(".next").fadeIn(); num = a(this).parent().index(); //獲取當前圖片的父元素的索引并賦給num為后邊點擊上一張、下一張服務 }); //上一張 a(".prev").click(function(){ if(num == 0){ num = numpic; } var src = obj.find("li").eq(num-1).find("img").attr("src"); a(".lg_img").attr("src",src); imgobj(a(".lg_img"), a(".lightbox")); num--; }); //下一張 a(".next").click(function(){ if(num == numpic-1){ num = -1; } var src = obj.find("li").eq(num+1).find("img").attr("src"); a(".lg_img").attr("src",src); imgobj(a(".lg_img"), a(".lightbox")); num++; }); //點擊除了上一張、下一張之外的其他地方隱藏 a(".lb_wrap").click(function(e){ var e = e || window.event; var elem = e.target || e.srcElement; while(elem){ if (elem.className && elem.className.indexOf('prev')>-1) { return; } if(elem.className && elem.className.indexOf('next')>-1){ return; } elem = elem.parentNode; } a(this).find("img").attr("src","loading.gif").hide(); //隱藏后,再將默認的圖片賦給lightbox中圖片的src a(this).find(".prev").hide().siblings(".next").hide(); a(this).fadeOut(); }); }) } })(jQuery);
Click here for complete example codeDownload from this site.
Readers who are interested in more jQuery-related content can check out the special topics on this site: "JQuery drag effects and skills summary", "jQuery extension skills summary", "JQuery common classic special effects summary", "jQuery animation and special effects usage summary" and "jquery selector usage summary"
I hope this article will be helpful to everyone in jQuery programming.

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

There are three common ways to initiate HTTP requests in Node.js: use built-in modules, axios, and node-fetch. 1. Use the built-in http/https module without dependencies, which is suitable for basic scenarios, but requires manual processing of data stitching and error monitoring, such as using https.get() to obtain data or send POST requests through .write(); 2.axios is a third-party library based on Promise. It has concise syntax and powerful functions, supports async/await, automatic JSON conversion, interceptor, etc. It is recommended to simplify asynchronous request operations; 3.node-fetch provides a style similar to browser fetch, based on Promise and simple syntax

JavaScript data types are divided into primitive types and reference types. Primitive types include string, number, boolean, null, undefined, and symbol. The values are immutable and copies are copied when assigning values, so they do not affect each other; reference types such as objects, arrays and functions store memory addresses, and variables pointing to the same object will affect each other. Typeof and instanceof can be used to determine types, but pay attention to the historical issues of typeofnull. Understanding these two types of differences can help write more stable and reliable code.

Which JavaScript framework is the best choice? The answer is to choose the most suitable one according to your needs. 1.React is flexible and free, suitable for medium and large projects that require high customization and team architecture capabilities; 2. Angular provides complete solutions, suitable for enterprise-level applications and long-term maintenance; 3. Vue is easy to use, suitable for small and medium-sized projects or rapid development. In addition, whether there is an existing technology stack, team size, project life cycle and whether SSR is needed are also important factors in choosing a framework. In short, there is no absolutely the best framework, the best choice is the one that suits your needs.

Hello, JavaScript developers! Welcome to this week's JavaScript news! This week we will focus on: Oracle's trademark dispute with Deno, new JavaScript time objects are supported by browsers, Google Chrome updates, and some powerful developer tools. Let's get started! Oracle's trademark dispute with Deno Oracle's attempt to register a "JavaScript" trademark has caused controversy. Ryan Dahl, the creator of Node.js and Deno, has filed a petition to cancel the trademark, and he believes that JavaScript is an open standard and should not be used by Oracle

Promise is the core mechanism for handling asynchronous operations in JavaScript. Understanding chain calls, error handling and combiners is the key to mastering their applications. 1. The chain call returns a new Promise through .then() to realize asynchronous process concatenation. Each .then() receives the previous result and can return a value or a Promise; 2. Error handling should use .catch() to catch exceptions to avoid silent failures, and can return the default value in catch to continue the process; 3. Combinators such as Promise.all() (successfully successful only after all success), Promise.race() (the first completion is returned) and Promise.allSettled() (waiting for all completions)

CacheAPI is a tool provided by the browser to cache network requests, which is often used in conjunction with ServiceWorker to improve website performance and offline experience. 1. It allows developers to manually store resources such as scripts, style sheets, pictures, etc.; 2. It can match cache responses according to requests; 3. It supports deleting specific caches or clearing the entire cache; 4. It can implement cache priority or network priority strategies through ServiceWorker listening to fetch events; 5. It is often used for offline support, speed up repeated access speed, preloading key resources and background update content; 6. When using it, you need to pay attention to cache version control, storage restrictions and the difference from HTTP caching mechanism.

JavaScript array built-in methods such as .map(), .filter() and .reduce() can simplify data processing; 1) .map() is used to convert elements one to one to generate new arrays; 2) .filter() is used to filter elements by condition; 3) .reduce() is used to aggregate data as a single value; misuse should be avoided when used, resulting in side effects or performance problems.

JavaScript's event loop manages asynchronous operations by coordinating call stacks, WebAPIs, and task queues. 1. The call stack executes synchronous code, and when encountering asynchronous tasks, it is handed over to WebAPI for processing; 2. After the WebAPI completes the task in the background, it puts the callback into the corresponding queue (macro task or micro task); 3. The event loop checks whether the call stack is empty. If it is empty, the callback is taken out from the queue and pushed into the call stack for execution; 4. Micro tasks (such as Promise.then) take precedence over macro tasks (such as setTimeout); 5. Understanding the event loop helps to avoid blocking the main thread and optimize the code execution order.
