<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>jQuery &#8211; WiredPrairie</title>
	<atom:link href="blog/archives/tag/jquery/feed" rel="self" type="application/rss+xml" />
	<link>/blog</link>
	<description>Yet another tech blog.</description>
	<lastBuildDate>Thu, 15 Mar 2012 12:55:05 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0</generator>
<site xmlns="com-wordpress:feed-additions:1">193486638</site>	<item>
		<title>JavaScript: isScrolledIntoView</title>
		<link>/blog/index.php/archives/1570</link>
		
		<dc:creator><![CDATA[Aaron]]></dc:creator>
		<pubDate>Thu, 15 Mar 2012 12:55:05 +0000</pubDate>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[SmugMug]]></category>
		<guid isPermaLink="false">/blog/?p=1570</guid>

					<description><![CDATA[I needed a simple way to detect when a placeholder DIV (that would contain an image) had scrolled into the current viewport of the browser. I’ve seen a few solutions that worked, and a few that didn’t (hello? test your code!). Here’s my simple solution: function isScrolledIntoView(elem) { elem = $(elem); var win = $(window); [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><img loading="lazy" style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: right; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" align="right" src="blog/wpcontent/uploads/2012/03/image4.png" width="242" height="133" /></p>
<p>I needed a simple way to detect when a placeholder DIV (that would contain an image) had scrolled into the current viewport of the browser. I’ve seen a few solutions that worked, and a few that didn’t (hello? test your code!). Here’s my simple solution:</p>
<pre class="code"><span style="color: blue">function </span>isScrolledIntoView(elem) {
    elem = $(elem);
    <span style="color: blue">var </span>win = $(window);
    <span style="color: blue">var </span>docViewTop = win.scrollTop();
    <span style="color: blue">var </span>docViewBottom = docViewTop + win.height();
    <span style="color: blue">var </span>elemTop = elem.offset().top;
    <span style="color: blue">var </span>elemBottom = elemTop + elem.height();

    <span style="color: blue">var </span>a = (elemTop &gt; docViewBottom);
    <span style="color: blue">if </span>(a) { <span style="color: blue">return false</span>; }
    <span style="color: blue">var </span>b = (elemBottom &gt; docViewTop);
    <span style="color: blue">if </span>(!b) { <span style="color: blue">return false</span>; }
    <span style="color: blue">return </span>!(a &amp;&amp; b);
}</pre>
<p>Nothing fancy, except it does require jQuery.</p>
<p>When one of the place holder DIVs scrolled into view, the code would trigger a queued load of the image.</p>
<p><img loading="lazy" style="display: inline" title="SmugMupBrowser-live" alt="SmugMupBrowser-live" src="blog/wpcontent/uploads/2012/03/SmugMupBrowser-live.gif" width="536" height="332" /></p>
<p>As the SmugMug API is poorly designed in a few places, in particular as it relates to showing a thumbnail for a gallery/album, when an album/gallery is scrolled into view, the app loads a list of ALL of the images from the now visible album and selects one of them to show as the thumbnail.<strong> It’s extremely inefficient unfortunately. </strong>Instead, if they could have included that extra piece of data in the gallery list (thumbnail image and thumbnail image key), boom! </p>
<p>I’ve also included a delay so that the auto loading only occurs after a 1 second pause (either the window being resized or scrolling occurring):</p>
<pre class="code"><span style="color: blue">function </span>delayLoadNewVisible() {
    <span style="color: blue">if </span>(visibilityDelayTimerId == 0) {
        visibilityDelayTimerId = window.setTimeout(<span style="color: blue">function </span>() {
            visibilityDelayTimerId = 0;
            loadNewVisible();
        }, 1000);
    }
}</pre>
<p>I’m not going to post the code for the other useful aspect of this functionality, but I’ll tell you about it, and leave coding it as an exercise for the reader. <img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="blog/wpcontent/uploads/2012/03/wlEmoticon-smile2.png" /></p>
<p>When the user scrolls new albums into view, after the pause, they’re added to a load queue. However, as it’s just as likely that the user will scroll the album off the screen, my load code also can remove something from the load queue if it hasn’t been loaded already. This way, visible albums are given precedence. By using the queue in this way, there are a manageable number of outstanding requests at any given time, and ideally only those that are relevant to what’s on the user’s screen. </p>
]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1570</post-id>	</item>
	</channel>
</rss>
