-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* separate CSS and Javascript from the layout file * remove date in search page * avoid exposing global functions * only select iframes that have "data-src" attributes
- Loading branch information
Showing
4 changed files
with
57 additions
and
34 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
--- | ||
title: 搜索 | ||
date: '2009-03-15T23:48:26+00:00' | ||
weight: 8 | ||
menu: main | ||
--- | ||
|
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
(function() { | ||
|
||
function show_iframe(iframe_elem) { | ||
if (iframe_elem.hasAttribute("src")) { | ||
return; | ||
} | ||
iframe_elem.src = iframe_elem.getAttribute("data-src"); | ||
iframe_elem.style.height = "700px"; | ||
iframe_elem.style.visibility = "visible"; | ||
} | ||
|
||
// https://stackoverflow.com/questions/487073/check-if-element-is-visible-after-scrolling/488073#488073 | ||
function on_screen(elem) { | ||
var elem_top = elem.getBoundingClientRect().top; | ||
var elem_bot = elem.getBoundingClientRect().bottom; | ||
var on_scr = (elem_top >= 0) && (elem_bot <= window.innerHeight); | ||
return on_scr; | ||
} | ||
|
||
function iframe_delayed_loading(iframe_elem) { | ||
// If the element is already on the current screen, start loading it | ||
if (on_screen(iframe_elem)) { | ||
show_iframe(iframe_elem); | ||
} else { | ||
// Otherwise, attach the action to the scroll event | ||
function listener() { | ||
if (on_screen(iframe_elem)) { | ||
// Run only once | ||
document.removeEventListener("scroll", listener); | ||
show_iframe(iframe_elem); | ||
} | ||
} | ||
document.addEventListener("scroll", listener); | ||
} | ||
} | ||
|
||
// Find all iframes | ||
var iframes = document.querySelectorAll("iframe[data-src]"); | ||
var len = iframes.length; | ||
var i; | ||
for (i = 0; i < len; i++) { | ||
iframe_delayed_loading(iframes[i]); | ||
} | ||
|
||
})(); |