Last active
December 29, 2015 17:29
-
-
Save jakubgg/7704886 to your computer and use it in GitHub Desktop.
Inject link and redirect for specific slide for Wordpress NextGEN gallery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame; | |
/** | |
* Inject link and redirect for specific slide for Wordpress NextGEN gallery | |
* (c) 2013 Code Lens | Jakub Gadkowski | |
* Licenced under Creative Commons Attribution 4.0 International Public License http://creativecommons.org/licenses/by/4.0/ | |
*/ | |
window.addEventListener('load', function(){ | |
var CLtimeLast = Date.now(); //create initial timestamp | |
var CLtestedSLider = document.getElementById('slider'); //monitoring this element | |
var CLstateIndicator = 1; //starting from the first slide so we need it on | |
//start checks only if monitored element exist | |
if (CLtestedSLider) { | |
function CLupdateSlider(internalCheck){ | |
//if we are on the right slide and new styles has not been applied | |
if (CLstateIndicator === 1 && internalCheck === 1 ) { | |
//apply new style and add 'click' listener | |
CLtestedSLider.style.cursor = 'pointer'; | |
CLtestedSLider.addEventListener('click',CLredirect); | |
//reset indicator so it will not fire again on this slide | |
CLstateIndicator = 0; | |
}else if (CLstateIndicator === 0 && internalCheck === 0) { | |
//remove style and remove eventListener | |
CLtestedSLider.style.cursor = 'default'; | |
CLtestedSLider.removeEventListener('click',CLredirect); | |
//reset indicator, it will fire when righ slide shows again | |
CLstateIndicator = 1; | |
} | |
} | |
function CLredirect(){ | |
window.open('http://putyourlinkhere'); | |
} | |
//do the check using rAF - less resources needed than setInterval | |
function CLcheckSlider(){ | |
//calculate delta | |
var dateNow = Date.now(); | |
var delta = dateNow - CLtimeLast; | |
//do the heavy check only once per second | |
if (delta >= 1000){ | |
CLtimeLast = dateNow; | |
// the [0] corresponds with the first slide, the second will be [1], third = [2] | |
var CLelementTest = CLtestedSLider.getElementsByClassName('nivo-control')[0].classList.contains('active'); | |
//if we are on the right slide fire up style changes | |
if ( CLelementTest ){ | |
CLupdateSlider(1); | |
}else { | |
//or tell the CLupdateSlider to revert changes | |
CLupdateSlider(0); | |
} | |
} | |
//schedule next rAF | |
requestAnimationFrame(CLcheckSlider); | |
} | |
//fire up first rAF | |
requestAnimationFrame(CLcheckSlider); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment