"Performance optimization" lazy, lazy loading, lazy animation---Update the DOM's view state in real-time based on the window scrolling event to indicate whether the specified visual area is in a defined and change callback

Source: Internet
Author: User

/* * Extended JQ prototype: lazy, lazy loading, lazy animation *---Update the view state of the DOM in real time based on the window scrolling event to indicate whether the specified viewable area is in the defined visual region and make a change callback * $jq. ViewChange ({top:100,bottom:100 , Change:function (status) {}})---Status: ' in '/' out ' * $jq. ViewChange (' off ') * * $JQ [n].viewchange.status = ' "/' out '--- --The state ' in ' indicates that the DOM is in the specified visible area * */(function () {var $win = $ (window), var Winh = $win. Height (), var _isrun = true; $win. Resize (funct Ion () {Winh = $win. Height (), if (_isrun) {_isrun = False;settimeout (function () {$win. Trigger (' Scroll.viewchange '); _ Isrun = true;}, 100);}); var setStatus = function (params) {if (this.parentelement) {///determines if the DOM is still in the page var $self = $ (this); var offsetTop = $self. Offset (). TOP,SELFH = $self. Height (), scrolltop = $win. scrolltop (); if (scrolltop + Winh + params.bottom > OffsetTop && Scrolltop-params.top < OffsetTop + selfh) {if (This.viewChange.status!== ' in ') {this.viewChange.status = ' in ';p arams . Change.call (this, this.viewChange.status);}} else {if (this.viewChange.status!== ' out ') {this.viewChange.status = ' out ';p arams.cHange.call (this, this.viewChange.status);}}} else {$win. Off (' scroll. ' + This.viewChange.eventNamespace);}}; $.fn.viewchange = function (options) {if (options = = = ' off ') {This.each (function () {if (This.viewchange) {$win. Off (' Scroll. ' + this.viewChange.eventNamespace);d elete This.viewchange;}});} else {var params = {top:0,//increase or decrease the top range bottom:0,//increase or decrease the bottom range change: $.noop//Callback when the view state changes: function (status) {}};p Arams = $. Extend (params, options);p arams.top = parseint (params.top);p arams.bottom = parseint (Params.bottom); This.each ( function () {if (This.viewchange) {$win. Off (' scroll. ' + this.viewChange.eventNamespace);} This.viewchange = {eventnamespace: ' viewChange. ' + math.random (). toString (). Replace (' 0. ', '),}; $win. On (' scroll. ' + This.viewChange.eventNamespace, function () {Setstatus.call (this, params);}. Bind (this)); Setstatus.call (this, params);}); return this;};}) ();//Usage $jq.viewchange ({top:-100,//Zoom out Top range bottom:100,//Increase bottom range change:function (status) {if (status = = = ' in ') {// When scrolling to the specified visual area} else if(Status = = = ' Out ')  {//When scrolling out the specified viewable area}}});

  

"Performance optimization" lazy, lazy loading, lazy animation---Update the DOM's view state in real-time based on the window scrolling event to indicate whether the specified visual area is in a defined and change callback

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.