I’m not good at JavaScript and I found a snippet in order to highlight text using JQuery Color. It conflicts with Lightbox2 and I want to convert the snippet to JavaScript. Here’s my code:
function highlight(elemId){
var elem = $(elemId);
elem.css("backgroundColor", "#ffffff"); // hack for Safari
elem.animate({ backgroundColor: '#ffffaa' }, 0);
setTimeout(function(){$(elemId).animate({ backgroundColor: "#ffffff" }, 3000)},1000);
}
if (document.location.hash) {
highlight(document.location.hash);
}
$('a[href*="#"]').click(function() {
var elemId = '#' + $(this).attr('href').split('#')[1];
requestAnimationFrame(highlight(elemId));
});
How can I convert the code from JQuery to JavaScript?
2
Answers
Here is all the JavaScript you wanted.
You don’t need JavaScript if you need to apply once a highlight as long as the element is part of the pseudo-class :target
Otherwise, yes, JavaScript can help to achieve the desired goal.
Make sure to use (just like in the example above) a CSS class that will trigger CSS animation by using classList.add and classList.remove once the animation terminates.
Find out more about the methods and topics you want to research on MDN