ç®ã«è¦ããç»åãèªã¿è¾¼ã
ä½éã®ã¯ã©ã¤ã¢ã³ãããããã¢ãã¤ã«ã®ãã©ãã£ãã¯ãç¯ç´ãããã¨ãã¾ãããã
ãã®ç®çã«å¯¾ããç»åãããã«ã¯è¡¨ç¤ºããããããããã¬ã¼ã¹ãã«ãã«ç½®ãæãããã¨ã«æ±ºãã¾ãããæ¬¡ã®ããã«ãªãã¾ã:
<img src="placeholder.svg" width="128" height="128" data-src="real.jpg">
ãªã®ã§ãæåã¯ãã¹ã¦ã®ç»åã placeholder.svg ã§ããã¦ã¼ã¶ãç»åãè¦ããã¨ãã§ããä½ç½®ã¾ã§ãã¼ã¸ãã¹ã¯ãã¼ã«ãããã¨ãã-- src ã data-src ã®ãã®ã«å¤æ´ãã¾ããããããã¨ç»åãèªã¿è¾¼ã¾ãã¾ãã
iframe ã«ä¾ãããã¾ã:
ç»åãè¦ãããã«ã¹ã¯ãã¼ã«ããâè¦æ±ãããæ¬¡ç¬¬â èªã¿è¾¼ã¿ã¾ãã
è¦ä»¶:
- ãã¼ã¸ãèªã¿è¾¼ã¾ããã¨ãã¹ã¯ãã¼ã«ããåã«ç»é¢ã«è¡¨ç¤ºããã¦ããç»åã¯ããã«èªã¿è¾¼ã¾ãã¾ãã
- ç»åã®ä¸ã«ã¯ã
data-srcããªããã®ãããã¾ãã ã³ã¼ãã¯ãããã«è§¦ããã¹ãã§ã¯ããã¾ããã - 䏿¦ãã¼ããããç»åã¯ãã¹ã¯ãã¼ã«ã§ç»é¢å /å¤ãå¤ãã£ã¦ããã以ä¸åèªè¾¼ãããã¹ãã§ã¯ããã¾ããã
P.S. å¯è½ã§ããã°ãç¾å¨ã®ä½ç½®ã®1ãã¼ã¸åå¾ã«ããç»åããããªãã¼ããããããé«åº¦ãªè§£æ±ºçã使ãã¦ãã ããã
P.P.S. åç´ã¹ã¯ãã¼ã«ã®ã¿ãå¦çãããæ°´å¹³ã¹ã¯ãã¼ã«ã¯å¦çããã¾ããã
onscroll ãã³ãã©ã§ã©ã®ç»åãè¦ãã¦ããã®ãã確èªãããããã表示ããå¿
è¦ãããã¾ãã
ã¾ããã¹ã¯ãã¼ã«ãããåã«ããã«è¦ããç»åãæ¤ç¥ãã¦ããããèªã¿è¾¼ãããã«ããã¼ã¸ãèªã¿è¾¼ã¾ããæã«ããããå®è¡ãããã§ãã
// ...the page content is above...
function isVisible(elem) {
let coords = elem.getBoundingClientRect();
let windowHeight = document.documentElement.clientHeight;
// top elem edge is visible OR bottom elem edge is visible
let topVisible = coords.top > 0 && coords.top < windowHeight;
let bottomVisible = coords.bottom < windowHeight && coords.bottom > 0;
return topVisible || bottomVisible;
}
showVisible();
window.onscroll = showVisible;
è¦ãã¦ããç»åã«å¯¾ãã¦ãimg.dataset.src ãåããimg.src ã«å²ãå½ã¦ã¾ã(ã¾ã ãã¦ããªãå ´å)ã
P.S. ãã®è§£æ±ºçã¯ï¼ãã¼ã¸ä¸ä¸ã«ããç»åã âããªãã¼ãâ ãã isVisibe ã®ããªã¢ã³ããæã£ã¦ãã¾ã(ãã¼ã¸ã®é«ã㯠document.documentElement.clientHeight ã§ã)ã
