No jQuery please!
I would love not to use jQuery for this, because it's a big library and I only need to do this single thing:
I would like to add a short delay to a click event so I can fade elements off my page using CSS.
I could post all of the code I've tried so far, but you'd get bored. So here's the one that I think is the closest:
document.getElementsByTagName('a').onclick = function (e) {
// Delay setting the location for one second
setTimeout(function() {this.href}, 90000);
var animOut = document.getElementByClassName('animateOut');
animOut.className += 'out';
};
I've already ruled out using onbeforeunload
, so hijacking the click event seems to be the best way.
Once again, I know this would be a doddle in jQuery, but I would like to avoid it if it's possible.
Thanks so much for your answers.
Ben
UPDATE
Thanks to commenters guest271314 and The Alpha, I've settled on this approach, but still have yet to complete the puzzle.
window.onload = function(){
var links = document.getElementsByTagName('a');
for( var i=0,il = links.length; i< il; i ++ ){
links[i].onclick = clickHandler;
}
function clickHandler(event) {
event.preventDefault();
// Delay setting the location for one second
setTimeout(function() {
location.href = this.href;
}, 90000);
// add `s` to `Element`
var animOut = document.getElementsByClassName("animateOut");
// iterate `animOut` elements
for (var i = 0; i < animOut.length; i++) {
// add `out` `className` to `animOut` element at index `i`
animOut[i].classList.add("out");
};
};
};
Having to iterate over the a
tags was an addition I have learned from reading other posts (I'll link to them in a minute).
Unfortunately, the setTimeout
function doesn't seem to be working. I need to refine what's in this function, but don't know in what way.
Further assistance would be most welcome.
Thanks
Ben
There should be an
"s"
atdocument.getElementsByClassName
. To addclassName
to allanimateOut
elements can use afor
loop; changethis.href
towindow.location.href = e.target.href
if expected result is to navigate tohref
of clickeda
element; else leave asthis.href
is requirement is to refresh currentwindow.location.href
:this.href
withinsetTimeout
I can't really take credit for this, the 2 users below (guest271314 and The Alpha) deserve a lot of recognition for what they helped me to achieve. The full code, with a couple of refinements, is:
You may try something like this: