Skip to content

Instantly share code, notes, and snippets.

@jonaslund
Created April 2, 2020 15:59
Show Gist options
  • Save jonaslund/ce25ea979a62c18b3d915271ede6dfa0 to your computer and use it in GitHub Desktop.
Save jonaslund/ce25ea979a62c18b3d915271ede6dfa0 to your computer and use it in GitHub Desktop.
$(window).scroll(function(f) {
var scrolled = $(window).scrollTop();
$(".artist").each(function() {
if (isElementInViewport($(this))) {
if ($(this).attr("data-id") === "23") {} else {
if ($(this).attr("data-loaded") !== "1") {
var e = $(this).attr("data-src");
$(this).attr("data-loaded", 1);
$(this).find("iframe").attr("src", e);
}
}
} else {
if ($(this).attr("data-loaded") === "1") {
var g = $(this).attr("data-id");
if (g === "1" || g === "2") {} else {
$(this).find("iframe").attr("src", "");
$(this).attr("data-loaded", 0);
}
}
}
});
$(".thing").each(function() {
var g = $(this).attr("data-id");
if (isElementInViewport($(this))) {
if ($(this).attr("data-type") === "art") {
var e = $(".artmenu[data-id=" + g + "]");
} else {
var e = $(".txtmenu[data-id=" + g + "]");
}
$(e).show();
}
});
//distance from top to unload artwork1 and artwork2.
if (scrolled > 5000) {
unload($("#artwork1"));
unload($("#artwork2"));
}
});
function unload(e) {
var d = b(e).find("iframe");
b(d).attr("src", "");
b(e).attr("data-loaded", 0)
}
function isElementInViewport (el) {
// Special bonus for those using jQuery
if (typeof jQuery === "function" && el instanceof jQuery) {
el = el[0];
}
var rect = el.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && /* or $(window).height() */
rect.right <= (window.innerWidth || document.documentElement.clientWidth) /* or $(window).width() */
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment