Note that there are some explanatory texts on larger screens.

plurals
  1. POFind position of element, then do something in jquery using .position() not working
    primarykey
    data
    text
    <p>I can not seem to find a way of jQuery understanding the current position of an element. I scroll through some markup using the html and css, jQuery below (like a gallery - but I dont want to use any plugins) but need to find the last and first positions so it does not continiually let someone scroll content off the page - currently I am just colouring the parent container when applying an action but will swap this out in live to not show or grey out the pagination/scroll buttons. </p> <p>The scrolling works by adjusting by 209px or -209px just does not understand the position of first and last elements (li's). I am trying to stop the previous button working if there are no more previous li's and vice versa - like I said only colouring the parent for now for visual. There could be any number of li's in this as they get dynamically spat out. Attached fiddle URL too: <a href="http://jsfiddle.net/jambo/zLSUT/" rel="nofollow">http://jsfiddle.net/jambo/zLSUT/</a> Apologies for images not working in this demo. So to recap - need to stop pagination in the direction where there will be no more content to view. Hope that makes sense, thank you in advance.</p> <p>css</p> <pre><code> .smartH{ float: left; position: relative; width:10000px; height: 80px; padding: 5px 0; font-family: arial; background: #fff; } .smartH a{ float: left; text-decoration: none; cursor:pointer; } .smartH img{ float: left; width: 100px; height: 80px; border: none; } .smartH .title{ float: right; margin: 0 5px; width:90px; font-weight: bold; font-size: 13px; color: #000; line-height: 1.4; text-align: left; } .smartH .text{ float: right; margin: 5px 5px 0; width:90px; font-size: 12px; color: #000; line-height: 1.1; text-align: left; } .smartAd{ float:left; width:627px; overflow: hidden; } .smartH li{ position:relative; float:left; display: inline; height:80px; width:200px; padding:0 4px 0 5px; list-style-type: none; } .smartAd .paginate{ float:right } .smartAd i{ cursor:pointer; } </code></pre> <p>jQuery</p> <pre><code> $( function () { var posF = $(".smartH li.first").position(); var posL = $(".smartH li.last").position(); $("a#next").click( function () { $(".smartH li").stop().animate({ left: "-=209px", }, 500 ); if (posL.left == -627){ $(".smartH").css("background", "blue"); } return false; }); $("a#prev").click( function () { $(".smartH li").stop().animate({ left: "+=209px", }, 500 ); if (posF.left == 0){ $(".smartH").css("background", "green"); } return false; }); }); </code></pre> <p>html</p> <pre><code>&lt;div class="smartAd"&gt; &lt;ul class="smartH"&gt;&lt;!-- posit abs? --&gt; &lt;li class="first"&gt; &lt;a href="http://www.google.com" target="_blank"&gt; &lt;img src="http://advertising.co.uk/wallpaper/blinds.gif" alt="blinds" /&gt; &lt;p class="title"&gt;Call for a free quote today&lt;/p&gt; &lt;p class="text"&gt;Best value blinds in Lincolnshire!&lt;/p&gt; &lt;/a&gt; &lt;/li&gt; &lt;li&gt; &lt;a href="http://www.google.com" target="_blank"&gt; &lt;img src="http://advertising.co.uk/wallpaper/coach.gif" alt="coach" /&gt; &lt;p class="title"&gt;Experience great days out&lt;/p&gt; &lt;p class="text"&gt;Great value coach trips&lt;/p&gt; &lt;/a&gt; &lt;/li&gt; &lt;li&gt; &lt;a href="http://www.google.com" target="_blank"&gt; &lt;img src="http://advertising.co.uk/wallpaper/coffee.gif" alt="coffee" /&gt; &lt;p class="title"&gt;Need beans?&lt;/p&gt; &lt;p class="text"&gt;We have a great variety of beans at great prices&lt;/p&gt; &lt;/a&gt; &lt;/li&gt; &lt;li&gt; &lt;a href="http://www.google.com" target="_blank"&gt; &lt;img src="http://advertising.co.uk/wallpaper/creditcard.gif" alt="credit card" /&gt; &lt;p class="title"&gt;Got bad credit?&lt;/p&gt; &lt;p class="text"&gt;We can help you boost your rating&lt;/p&gt; &lt;/a&gt; &lt;/li&gt; &lt;li&gt; &lt;a href="http://www.google.com" target="_blank"&gt; &lt;img src="http://advertising.co.uk/wallpaper/mobility.gif" alt="mobility" /&gt; &lt;p class="title"&gt;Struggling to get around?&lt;/p&gt; &lt;p class="text"&gt;Great value scooters!&lt;/p&gt; &lt;/a&gt; &lt;/li&gt; &lt;li class="last"&gt; &lt;a href="http://www.google.com" target="_blank"&gt; &lt;img src="http://advertising.co.uk/wallpaper/plumber.gif" alt="plumber" /&gt; &lt;p class="title"&gt;Got a leak?&lt;/p&gt; &lt;p class="text"&gt;Ring for reliable and friendly plumbers&lt;/p&gt; &lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;&lt;!-- // ul.smartH --&gt; &lt;span class="paginate"&gt; &lt;a href="" class="icon" id="prev"&gt;Prev 1-&lt;/a&gt; &lt;a href="" class="icon" id="next"&gt;- Next 1&lt;/a&gt; &lt;/span&gt; &lt;/div&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload