<?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/"
        >
<channel>
  <title>asgaard</title>
  <description>Jquery</description>
  <link>https://blog.asgaard.co.uk/t/jquery</link>
  <lastBuildDate>Thu, 23 Apr 26 05:21:59 +0000</lastBuildDate>
  <language>en</language>
  <count>3</count>
  <offset>0</offset>
      <item>
    <title>Pseudo elements&#039; CSS with jQuery</title>
    <link>https://blog.asgaard.co.uk/2012/09/08/again-on-manipulating-css-pseudo-elements-with-jquery</link>
    <pubDate>Sat, 08 Sep 12 15:18:12 +0000</pubDate>
    <guid>https://blog.asgaard.co.uk/2012/09/08/again-on-manipulating-css-pseudo-elements-with-jquery</guid>
    <description><![CDATA[
<p>
Further to what I wrote <a href='http://blog.asgaard.co.uk/2012/09/07/on-targeting-pseudo-elements-with-javascript-jquery'>yesterday</a> about this, I&#039;ve written a jQuery plugin to provide a nice interface to pseudo-element CSS changes. Basically it *kind of* allows you to target pseudo-elements for the sake of CSS manipulation. It looks something like this:
<p>
<pre>$('#element').pseudoCss(':before', 'background-color', 'red');</pre>
<p>
Check out the code from <a href='https://github.com/markwatkinson/jquery-pseudo-css' target='_blank'>GitHub</a>. The index.html contains documentation, which you should be able to see online via <a href='http://htmlpreview.github.com/?https://raw.github.com/markwatkinson/jquery-pseudo-css/master/index.html' target='_blank'>this link</a>.
<p>
Please do read the caveat section before you use it. It should work for most situations but it is inherently limited compared to $.css().[...]]]></description>
    <content:encoded><![CDATA[
<p>
Further to what I wrote <a href='http://blog.asgaard.co.uk/2012/09/07/on-targeting-pseudo-elements-with-javascript-jquery'>yesterday</a> about this, I&#039;ve written a jQuery plugin to provide a nice interface to pseudo-element CSS changes. Basically it *kind of* allows you to target pseudo-elements for the sake of CSS manipulation. It looks something like this:
<p>
<pre>$('#element').pseudoCss(':before', 'background-color', 'red');</pre>
<p>
Check out the code from <a href='https://github.com/markwatkinson/jquery-pseudo-css' target='_blank'>GitHub</a>. The index.html contains documentation, which you should be able to see online via <a href='http://htmlpreview.github.com/?https://raw.github.com/markwatkinson/jquery-pseudo-css/master/index.html' target='_blank'>this link</a>.
<p>
Please do read the caveat section before you use it. It should work for most situations but it is inherently limited compared to $.css().]]></content:encoded>
  </item>
      <item>
    <title>On targeting pseudo-elements with JavaScript/jQuery</title>
    <link>https://blog.asgaard.co.uk/2012/09/07/on-targeting-pseudo-elements-with-javascript-jquery</link>
    <pubDate>Fri, 07 Sep 12 17:18:47 +0000</pubDate>
    <guid>https://blog.asgaard.co.uk/2012/09/07/on-targeting-pseudo-elements-with-javascript-jquery</guid>
    <description><![CDATA[<aside><strong>Edit</strong>: I&#039;ve written a jQuery plugin to automate some of this for you. If you need this on a bigger than trivial scale, it might be worth checking out! See: <a href='http://htmlpreview.github.com/?https://raw.github.com/markwatkinson/jquery-pseudo-css/master/index.html' target='_blank'>Docs/demo</a> | <a href='https://github.com/markwatkinson/jquery-pseudo-css' target='_blank'>code</a>.
<br>
</aside>
<p>
Pseudo-elements are pretty nice things you can inject into HTML elements via CSS.
<p>
<pre>#container { 
    position: relative;
}
#container::before {
    position: absolute;
    top: 0;
    left: 0;
    display: block;
    content: &quot;I am a block element in the top-left corner of #container and I don't exist in the markup&quot;;
}</pre>
<p>
They are very useful for adding presentational output to your DOM tree without needing to pollute the markup. But they are a bit of a pain because they don&#039;t really exist in the DOM, so accessing them programmatically with JavaScript is hard, or I think, impossible in the general case.
<p>
But depending on what you want to do, you might be able to achieve the result you want by being a bit clever... 
<p>
Usually you want to target this element because you intend to manipulate its CSS. You can still manipula[...]]]></description>
    <content:encoded><![CDATA[<aside><strong>Edit</strong>: I&#039;ve written a jQuery plugin to automate some of this for you. If you need this on a bigger than trivial scale, it might be worth checking out! See: <a href='http://htmlpreview.github.com/?https://raw.github.com/markwatkinson/jquery-pseudo-css/master/index.html' target='_blank'>Docs/demo</a> | <a href='https://github.com/markwatkinson/jquery-pseudo-css' target='_blank'>code</a>.
<br>
</aside>
<p>
Pseudo-elements are pretty nice things you can inject into HTML elements via CSS.
<p>
<pre>#container { 
    position: relative;
}
#container::before {
    position: absolute;
    top: 0;
    left: 0;
    display: block;
    content: &quot;I am a block element in the top-left corner of #container and I don't exist in the markup&quot;;
}</pre>
<p>
They are very useful for adding presentational output to your DOM tree without needing to pollute the markup. But they are a bit of a pain because they don&#039;t really exist in the DOM, so accessing them programmatically with JavaScript is hard, or I think, impossible in the general case.
<p>
But depending on what you want to do, you might be able to achieve the result you want by being a bit clever... 
<p>
Usually you want to target this element because you intend to manipulate its CSS. You can still manipulate its CSS! You just have to take the indirect and less commonly trodden route of using JS to create a stylesheet and writing raw rules into it. Example:
<p>
<pre>$('head').append(document.createElement('style'));
var myStyleSheet = document.styleSheets[document.styleSheets.length-1];

var myRule = &quot;#container::before { content: \&quot;Let's change my text\&quot;; }&quot;;
if (myStyleSheet.insertRule) {
    myStyleSheet.insertRule(myRule, myStyleSheet.cssRules.length);
}</pre>
<p>
What we&#039;ve done in the code above is to create a dedicated stylesheet for this and then injected a rule which should override the previous rule (assuming selector specifity is equal, but let&#039;s not get into that). This is obviously not as neat as simply using jQuery.css(), but we don&#039;t have too many options, so let&#039;s focus on managing the situation we&#039;ve inherited.<h3>Cool story bro, but how do I scale this?</h3>
<p>
Exactly what strategy you use to lay your code out depends on your exact problem. If you only need to consider changes to one element, just keep it simple.
<p>
But if you need to handle multiple elements individually you have to manage problems selecting them and targeting them individually. If they don&#039;t already have an ID set, you&#039;ll have to create IDs on them and use that in the rule&#039;s selector. The second parameter of insertRule is an index. We can use this to help us out; we can iterate over each of our elements initially and assign it an index (use $.data()), insert a no-op rule at that index to force it to exist, and then make sure that element may only write to that index. After doing so, we&#039;ll still have to remove the old rule which will now be in index+1, which we can do with <code>myStyleSheet.deleteRule(index+1)</code>.
<p>
I&#039;ve made a <a href='http://jsfiddle.net/BtM7g/2/'>fiddle</a> to show roughly how it works. It by no means handles all feasible cases but should give you an idea how to lay things out.]]></content:encoded>
  </item>
      <item>
    <title>Figure out which element is scrolled to jQuery</title>
    <link>https://blog.asgaard.co.uk/2012/04/23/figure-out-which-element-is-scrolled-to-in-the-viewport-with-jquery</link>
    <pubDate>Mon, 23 Apr 12 20:39:46 +0000</pubDate>
    <guid>https://blog.asgaard.co.uk/2012/04/23/figure-out-which-element-is-scrolled-to-in-the-viewport-with-jquery</guid>
    <description><![CDATA[
<p>
Does what it says on the tin. It takes a set of elements and tries to determine which one is featured most predominantly on the page. Change the $sections selector (line 3) to target the element-set of your choice, or rewrite as a plugin :)  
<p>
Towards the bottom, the element which is deemed to be in view will be given a &#039;highlight&#039; class.
<p>
Note that we are binding directly to the scroll event which is generally a bad idea. Whether it is a bad idea for you depends upon various factors. If you find it negative impacts performance on your page, factor out the scroll event and call it after a short timer (and cancel the timer on the next scroll).
<p>
<pre>(function() {
  &quot;use strict&quot;;
  var $sections = $('.panel'),
      $sectionsReversed = $($sections.get().reverse()),
      $last = $({});
  
  $(window).scroll(function(ev) {
    var pos = $(document).scrollTop(), 
        $nearest = $sections.eq(0),
        amount = 0; // amount of the screen occupied
        
    // scrolled to extremes - take th</pre>[...]]]></description>
    <content:encoded><![CDATA[
<p>
Does what it says on the tin. It takes a set of elements and tries to determine which one is featured most predominantly on the page. Change the $sections selector (line 3) to target the element-set of your choice, or rewrite as a plugin :)  
<p>
Towards the bottom, the element which is deemed to be in view will be given a &#039;highlight&#039; class.
<p>
Note that we are binding directly to the scroll event which is generally a bad idea. Whether it is a bad idea for you depends upon various factors. If you find it negative impacts performance on your page, factor out the scroll event and call it after a short timer (and cancel the timer on the next scroll).
<p>
<pre>(function() {
  &quot;use strict&quot;;
  var $sections = $('.panel'),
      $sectionsReversed = $($sections.get().reverse()),
      $last = $({});
  
  $(window).scroll(function(ev) {
    var pos = $(document).scrollTop(), 
        $nearest = $sections.eq(0),
        amount = 0; // amount of the screen occupied
        
    // scrolled to extremes - take the top or bottom element as appropriate
    // There's a little bit of leeway on the bottom scroll detection as 
    // Firefox sometimes seems to lose a pixel.
    if (pos === 0 &amp;&amp; false)
      $nearest = $sections.eq(0);
    else if (pos &gt;= $(document).height() - $(window).height() - 10) {
      $nearest = $sectionsReversed.eq(0);
    }
    // otherwise do something a little cleverer
    else {
      var windowHeight = $(window).height(),
          start = pos,
          end = start + windowHeight;
      $sections.each(function() {
        // we're going to try to figure out which element
        // takes up the important parts of the screen.
        var $this = $(this),
            top = $this.offset()? $this.offset().top + $this.outerHeight() : //IE?
              $this.offset().top,
            bottom = $this.offset().top + $this.outerHeight(),
            topVisible = top &gt;= start &amp;&amp; top &lt;= end,
            bottomVisible = bottom &gt;= start &amp;&amp; bottom &lt;= end,
            candidateAmount = 0;
            

        if (top &lt;= start &amp;&amp; bottom &gt;= end) {
          // occupies entire screen
          // we have a winner
          $nearest = $this;
          amount = 1;
          return false;
        }
        // no part of the element visible - nope
        else if (!topVisible &amp;&amp; !bottomVisible) return;
        
        else if (topVisible &amp;&amp; bottomVisible) {
          // full element is visible. We'll take this one
          // since it's highest on the screen.
          amount = (bottom-top)/windowHeight;
          $nearest = $this;
          return false;
        }
        else if (bottomVisible) {
          // occupies part of the screen from the top down only
          candidateAmount = (bottom - start)/windowHeight;
        }
        else if (topVisible) {
          // occupies part of the screen from the bottom up only
          candidateAmount = (end - top)/windowHeight;
        }
        
        if (candidateAmount &gt; amount) {
          amount = candidateAmount;
          $nearest = $this;
        }
      });
    }
    
    if ($last !== $nearest) {
      $last.removeClass('highlight');
      $nearest.addClass('highlight');
      $last = $nearest;
    }
  });
  $(window).trigger('scroll');
})();</pre>]]></content:encoded>
  </item>
  </channel>
</rss>