Skip to content

Instantly share code, notes, and snippets.

@fernandosavio
Last active January 26, 2017 13:29
Show Gist options
  • Save fernandosavio/0d4678bf76e273386782e4defe945121 to your computer and use it in GitHub Desktop.
Save fernandosavio/0d4678bf76e273386782e4defe945121 to your computer and use it in GitHub Desktop.
Line highlighting for embedded Gists.
// jQuery version
$(document).on('click', '.js-line-number', function(event){
var elem = $(this).next('.js-file-line'),
bg_color = elem.css('backgroundColor'),
color_active = 'rgb(248, 238, 199)';
elem.css('backgroundColor', bg_color === color_active ? 'rgba(0, 0, 0, 0)' : color_active);
});
var elem = $(document.location.hash).trigger('click');
if (elem.length) {
$('html, body').animate({
scrollTop: elem.offset().top
}, 800);
}
// Vanilla.js version
document.addEventListener('click', function (event) {
var elem = event.target, style;
if (elem.classList.contains('js-line-number')) {
style = elem.nextElementSibling.style;
style.backgroundColor = !!style.backgroundColor ? '' : 'rgb(248, 238, 199)';
}
});
var elem = document.querySelector(document.location.hash);
if (!!elem) {
elem.click();
elem.scrollIntoView(false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment