JQuery plug-in's article image pop-up amplification effect, jquery plug-in pop-up amplification effect

Source: Internet
Author: User

JQuery plug-in's article image pop-up amplification effect, jquery plug-in pop-up amplification effect

First, write a basic format:

$.fn.popImg = function() {    //your code goes here}

Then wrap your code with a self-called anonymous function and pass the system variables to the plug-in the form of variables, as shown below:

;(function($,window,document,undefined){    $.fn.popImg = function() {      //your code goes here    }})(jQuery,window,document);

Next, click the article image to bring up the image and enlarge it.

The overall code is as follows:

; (Function ($, window, document, undefined) {$. fn. popImg = function () {// create a pop-up layer var $ layer = $ ("<div>" placement .css ({position: 'fixed', left: 0, right: 0, top: 0, bottom: 0, width: '000000', height: '000000', zIndex: 100%, display: 'none', background: "#000", opacity: '0. 6'}); // copy the clicked image to obtain the width and height of the image and the position var cloneImg = function ($ targetImg) {var cloneW = $ targetImg. width (), cloneH = $ targetImg. height (), left = $ targetImg. offset (). left, top = $ targetImg. offset (). top; return response targetimg.clone().css ({position: 'fixed', width: cloneW, height: cloneH, left: left, top: top, zIndex: 10000000 });}; // center the copied image with var centerImg = function ($ cloneImg) {var dW = $ (window ). width (); var dH = $ (window ). height (); $cloneImg.css ('cursor ', 'zoom-out '). attr ('clone-bigImg ', true); var img = new Image (); img. onload = function () {$ cloneImg. stop (). animate ({width: this. width, height: this. height, left: (dW-this. width)/2, top: (dH-this. height)/2}, 300);} img. src = $ cloneImg. attr ('src');}; this. each (function () {iterator (this).css ('cursor ', 'zoom-in '). on ('click', function () {var $ body = $ ("body"); $ layer. appendTo ($ body); $ layer. fadeIn (300); var $ c = cloneImg ($ (this); $ c. appendTo ($ body); centerImg ($ c) ;}); var timer = null; $ (window ). on ("resize", function () {$ ("img [clone-bigImg]"). each (function () {var $ this = $ (this); timer & clearTimeout (timer); timer = setTimeout (function () {centerImg ($ this );}, 10) ;}); $ (window ). on ("click keydown", function (evt) {if (evt. type = "keydown" & evt. keyCode = 27) {$ layer. fadeOut (300); $ ("img [clone-bigImg]"). remove ();} var $ this = require (evt.tar get); if ($ this. attr ("clone-bigImg") {$ layer. fadeOut (300); $ ("img [clone-bigImg]"). remove () ;}}) ;}} (jQuery, window, document );

Reference: http://barretlee.com/blog/2015/09/19/jquery-plugin-for-alert-img/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.