Best JavaScript code snippet using wpt
jquery.inview.js
Source:jquery.inview.js
1/**2 * author Christopher Blum3 * - based on the idea of Remy Sharp, http://remysharp.com/2009/01/26/element-in-view-event-plugin/4 * - forked from http://github.com/zuk/jquery.inview/5 */6(function (factory) {7 if (typeof define == 'function' && define.amd) {8 // AMD9 define(['jquery'], factory);10 } else if (typeof exports === 'object') {11 // Node, CommonJS12 module.exports = factory(require('jquery'));13 } else {14 // Browser globals15 factory(jQuery);16 }17}(function ($) {18 var inviewObjects = [], viewportSize, viewportOffset,19 d = document, w = window, documentElement = d.documentElement, timer;20 $.event.special.inview = {21 add: function(data) {22 inviewObjects.push({ data: data, $element: $(this), element: this });23 // Use setInterval in order to also make sure this captures elements within24 // "overflow:scroll" elements or elements that appeared in the dom tree due to25 // dom manipulation and reflow26 // old: $(window).scroll(checkInView);27 //28 // By the way, iOS (iPad, iPhone, ...) seems to not execute, or at least delays29 // intervals while the user scrolls. Therefore the inview event might fire a bit late there30 //31 // Don't waste cycles with an interval until we get at least one element that32 // has bound to the inview event.33 if (!timer && inviewObjects.length) {34 timer = setInterval(checkInView, 250);35 }36 },37 remove: function(data) {38 for (var i=0; i<inviewObjects.length; i++) {39 var inviewObject = inviewObjects[i];40 if (inviewObject.element === this && inviewObject.data.guid === data.guid) {41 inviewObjects.splice(i, 1);42 break;43 }44 }45 // Clear interval when we no longer have any elements listening46 if (!inviewObjects.length) {47 clearInterval(timer);48 timer = null;49 }50 }51 };52 function getViewportSize() {53 var mode, domObject, size = { height: w.innerHeight, width: w.innerWidth };54 // if this is correct then return it. iPad has compat Mode, so will55 // go into check clientHeight/clientWidth (which has the wrong value).56 if (!size.height) {57 mode = d.compatMode;58 if (mode || !$.support.boxModel) { // IE, Gecko59 domObject = mode === 'CSS1Compat' ?60 documentElement : // Standards61 d.body; // Quirks62 size = {63 height: domObject.clientHeight,64 width: domObject.clientWidth65 };66 }67 }68 return size;69 }70 function getViewportOffset() {71 return {72 top: w.pageYOffset || documentElement.scrollTop || d.body.scrollTop,73 left: w.pageXOffset || documentElement.scrollLeft || d.body.scrollLeft74 };75 }76 function checkInView() {77 if (!inviewObjects.length) {78 return;79 }80 var i = 0, $elements = $.map(inviewObjects, function(inviewObject) {81 var selector = inviewObject.data.selector,82 $element = inviewObject.$element;83 return selector ? $element.find(selector) : $element;84 });85 viewportSize = viewportSize || getViewportSize();86 viewportOffset = viewportOffset || getViewportOffset();87 for (; i<inviewObjects.length; i++) {88 // Ignore elements that are not in the DOM tree89 if (!$.contains(documentElement, $elements[i][0])) {90 continue;91 }92 var $element = $($elements[i]),93 elementSize = { height: $element[0].offsetHeight, width: $element[0].offsetWidth },94 elementOffset = $element.offset(),95 inView = $element.data('inview');96 // Don't ask me why because I haven't figured out yet:97 // viewportOffset and viewportSize are sometimes suddenly null in Firefox 5.98 // Even though it sounds weird:99 // It seems that the execution of this function is interferred by the onresize/onscroll event100 // where viewportOffset and viewportSize are unset101 if (!viewportOffset || !viewportSize) {102 return;103 }104 if (elementOffset.top + elementSize.height > viewportOffset.top &&105 elementOffset.top < viewportOffset.top + viewportSize.height &&106 elementOffset.left + elementSize.width > viewportOffset.left &&107 elementOffset.left < viewportOffset.left + viewportSize.width) {108 if (!inView) {109 $element.data('inview', true).trigger('inview', [true]);110 }111 } else if (inView) {112 $element.data('inview', false).trigger('inview', [false]);113 }114 }115 }116 $(w).on("scroll resize scrollstop", function() {117 viewportSize = viewportOffset = null;118 });119 // IE < 9 scrolls to focused elements without firing the "scroll" event120 if (!documentElement.addEventListener && documentElement.attachEvent) {121 documentElement.attachEvent("onfocusin", function() {122 viewportOffset = null;123 });124 }...
progress-bar.js
Source:progress-bar.js
1(function($) {2 // inView3 var inviewObjects = [],4 viewportSize, viewportOffset,5 d = document,6 w = window,7 documentElement = d.documentElement,8 timer9 $.event.special.inview = {10 add: function(data) {11 inviewObjects.push({12 data: data,13 $element: $(this),14 element: this15 })16 if (!timer && inviewObjects.length) {17 timer = setInterval(checkInView, 250)18 }19 },20 remove: function(data) {21 for (var i = 0; i < inviewObjects.length; i++) {22 var inviewObject = inviewObjects[i]23 if (inviewObject.element === this && inviewObject.data.guid === data.guid) {24 inviewObjects.splice(i, 1)25 break26 }27 }28 if (!inviewObjects.length) {29 clearInterval(timer)30 timer = null31 }32 }33 }34 function getViewportSize() {35 var mode, domObject, size = {36 height: w.innerHeight,37 width: w.innerWidth38 }39 if (!size.height) {40 mode = d.compatMode41 if (mode || !$.support.boxModel) { // IE, Gecko42 domObject = mode === 'CSS1Compat' ?43 documentElement : // Standards44 d.body // Quirks45 size = {46 height: domObject.clientHeight,47 width: domObject.clientWidth48 }49 }50 }51 return size52 }53 function getViewportOffset() {54 return {55 top: w.pageYOffset || documentElement.scrollTop || d.body.scrollTop,56 left: w.pageXOffset || documentElement.scrollLeft || d.body.scrollLeft57 }58 }59 function checkInView() {60 if (!inviewObjects.length) {61 return62 }63 var i = 0,64 $elements = $.map(inviewObjects, function(inviewObject) {65 var selector = inviewObject.data.selector,66 $element = inviewObject.$element67 return selector ? $element.find(selector) : $element68 })69 viewportSize = viewportSize || getViewportSize()70 viewportOffset = viewportOffset || getViewportOffset()71 for (; i < inviewObjects.length; i++) {72 if (!$.contains(documentElement, $elements[i][0])) {73 continue74 }75 var $element = $($elements[i]),76 elementSize = {77 height: $element[0].offsetHeight,78 width: $element[0].offsetWidth79 },80 elementOffset = $element.offset(),81 inView = $element.data('inview')82 if (!viewportOffset || !viewportSize) {83 return84 }85 if (elementOffset.top + elementSize.height > viewportOffset.top &&86 elementOffset.top < viewportOffset.top + viewportSize.height &&87 elementOffset.left + elementSize.width > viewportOffset.left &&88 elementOffset.left < viewportOffset.left + viewportSize.width) {89 if (!inView) {90 $element.data('inview', true).trigger('inview', [true])91 }92 } else if (inView) {93 $element.data('inview', false).trigger('inview', [false])94 }95 }96 }97 $(w).on("scroll resize scrollstop", function() {98 viewportSize = viewportOffset = null99 })100 if (!documentElement.addEventListener && documentElement.attachEvent) {101 documentElement.attachEvent("onfocusin", function() {102 viewportOffset = null103 })104 }105 // eaelProgressBar106 $.fn.eaelProgressBar = function() {107 var $this = $(this)108 var $layout = $this.data('layout')109 var $num = $this.data('count')110 var $duration = $this.data('duration')111 $this.one('inview', function() {112 if ($layout == 'line') {113 $('.eael-progressbar-line-fill', $this).css({114 'width': $num + '%',115 })116 } else if ($layout == 'half_circle') {117 $('.eael-progressbar-circle-half', $this).css({118 'transform': 'rotate(' + ($num * 1.8) + 'deg)',119 })120 }121 $('.eael-progressbar-count', $this).prop({122 'counter': 0123 }).animate({124 counter: $num125 }, {126 duration: $duration,127 easing: 'linear',128 step: function(counter) {129 if ($layout == 'circle') {130 var rotate = (counter * 3.6)131 $('.eael-progressbar-circle-half-left', $this).css({132 'transform': "rotate(" + rotate + "deg)",133 })134 if (rotate > 180) {135 $('.eael-progressbar-circle-pie', $this).css({136 'clip-path': 'inset(0)'137 })138 $('.eael-progressbar-circle-half-right', $this).css({139 'visibility': 'visible'140 })141 }142 }143 $(this).text(Math.ceil(counter))144 }145 })146 })147 }...
main.js
Source:main.js
1document.addEventListener('DOMContentLoaded', function () {2 const main = new Main();3});4class Main {5 constructor() {6 this.header = document.querySelector('.header');7 this.sides = document.querySelectorAll('.side');8 this._observers = [];9 this._init();10 }11 set observers(val) {12 this._observers.push(val);13 }14 get observers() {15 return this._observers;16 }17 _init() {18 new MobileMenu();19 this.hero = new HeroSlider('.swiper-container');20 Pace.on('done', this._paceDone.bind(this));21 }22 _paceDone() {23 this._scrollInit();24 }25 _inviewAnimation(el, inview) {26 if(inview) {27 el.classList.add('inview');28 }else {29 el.classList.remove('inview');30 }31 }32 _navAnimation(el, inview) {33 if(inview) {34 this.header.classList.remove('triggered');35 } else {36 this.header.classList.add('triggered');37 }38 }39 _sideAnimation(el, inview) {40 if(inview) {41 this.sides.forEach(side => side.classList.add('inview'));42 } else {43 this.sides.forEach(side => side.classList.remove('inview'));44 }45 }46 _textAnimation(el, inview) {47 if(inview) {48 const ta = new TweenTextAnimation(el);49 ta.animate();50 }51 }52 _toggleSlideAnimation(el, inview) {53 if(inview) {54 this.hero.start();55 } else {56 this.hero.stop();57 }58 }59 _destroyObservers() {60 this.observers.forEach(ob => {61 ob.destroy();62 });63 }64 destroy() {65 this._destroyObservers();66 }67 _scrollInit() {68 this.observers = new ScrollObserver('.nav-trigger', this._navAnimation.bind(this), {once: false});69 // this.observers = new ScrollObserver('.menu-open', this._inviewAnimation);70 this.observers = new ScrollObserver('.cover-slide', this._inviewAnimation);71 this.observers = new ScrollObserver('.opacity-animation', this._inviewAnimation);72 this.observers = new ScrollObserver('.opacity-simple', this._inviewAnimation);73 this.observers = new ScrollObserver('.appear', this._inviewAnimation);74 this.observers = new ScrollObserver('.tween-animate-title', this._textAnimation, {rootMargin: "-200px 0px"});75 this.observers = new ScrollObserver('.swiper-container', this._inviewAnimation);76 this.observers = new ScrollObserver('.swiper-container', this._toggleSlideAnimation.bind(this), {once: false});77 this.observers = new ScrollObserver('.search-form', this._inviewAnimation);78 this.observers = new ScrollObserver('.blog__category', this._inviewAnimation);79 this.observers = new ScrollObserver('.blog__items', this._inviewAnimation);80 this.observers = new ScrollObserver('.colum1', this._inviewAnimation);81 this.observers = new ScrollObserver('.colum2', this._inviewAnimation);82 this.observers = new ScrollObserver('.colum3', this._inviewAnimation);83 this.observers = new ScrollObserver('.colum4', this._inviewAnimation);84 this.observers = new ScrollObserver('.contact', this._inviewAnimation);85 this.observers = new ScrollObserver('#main-content', this._sideAnimation.bind(this), {once: false, rootMargin: "-300px 0px"});86 }...
Using AI Code Generation
1const wptools = require('wptools');2const wiki = wptools.page('Albert Einstein');3wiki.get((err, resp) => {4 if (err) {5 console.log(err);6 } else {7 console.log(resp);8 }9});10const wptools = require('wptools');11const wiki = wptools.page('Albert Einstein');12wiki.getImages((err, resp) => {13 if (err) {14 console.log(err);15 } else {16 console.log(resp);17 }18});19const wptools = require('wptools');20const wiki = wptools.page('Albert Einstein');21wiki.getLanguages((err, resp) => {22 if (err) {23 console.log(err);24 } else {25 console.log(resp);26 }27});28const wptools = require('wptools');29const wiki = wptools.page('Albert Einstein');30wiki.getLinks((err, resp) => {31 if (err) {32 console.log(err);33 } else {34 console.log(resp);35 }36});37const wptools = require('wptools');38const wiki = wptools.page('Albert Einstein');39wiki.getCategories((err, resp) => {40 if (err) {41 console.log(err);42 } else {43 console.log(resp);44 }45});46const wptools = require('wptools');47const wiki = wptools.page('Albert Einstein');48wiki.getTemplates((err, resp) => {49 if (err) {50 console.log(err);51 } else {52 console.log(resp);53 }54});55const wptools = require('wptools');56const wiki = wptools.page('Albert Einstein');57wiki.getReferences((err, resp) => {58 if (err) {59 console.log(err);60 } else {61 console.log(resp);62 }63});
Using AI Code Generation
1exports.test = function() {2 var inView = require('in-view');3 inView('.test').on('enter', function(el) {4 console.log('entered view', el);5 });6};
Using AI Code Generation
1function inView(element, threshold) {2 if (element === undefined) {3 element = window;4 }5 if (threshold === undefined) {6 threshold = 1;7 }8 if (element === window) {9 if (window.scrollY + window.innerHeight > document.body.scrollHeight * threshold) {10 return false;11 }12 return true;13 }14 if (element.getBoundingClientRect().top + element.getBoundingClientRect().height * threshold > window.innerHeight) {15 return false;16 }17 return true;18}
Using AI Code Generation
1var inView = function(selector, callback) {2 var element = document.querySelector(selector);3 var top = element.getBoundingClientRect().top;4 var bottom = element.getBoundingClientRect().bottom;5 var height = element.getBoundingClientRect().height;6 var windowHeight = window.innerHeight;7 var windowBottom = windowHeight;8 var windowTop = 0;9 var inView = false;10 if (height > windowHeight) {11 if (top < windowBottom && bottom > windowTop) {12 inView = true;13 }14 } else {15 if (top < windowBottom && top > windowTop) {16 inView = true;17 }18 }19 if (inView) {20 callback();21 }22 return inView;23};24var inView = function(selector, callback) {25 var element = document.querySelector(selector);26 var top = element.getBoundingClientRect().top;27 var bottom = element.getBoundingClientRect().bottom;28 var height = element.getBoundingClientRect().height;29 var windowHeight = window.innerHeight;30 var windowBottom = windowHeight;31 var windowTop = 0;32 var inView = false;33 if (height > windowHeight) {34 if (top < windowBottom && bottom > windowTop) {35 inView = true;36 }37 } else {38 if (top < windowBottom && top > windowTop) {39 inView = true;40 }41 }42 if (inView) {43 callback();44 }45 return inView;46};
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!