Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What if I told you you didn't have to use jQuery.animate at all to achieve the effect you want? Use <a href="https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Using_CSS_transitions" rel="nofollow">CSS3 transitions</a>! Here's a <a href="http://jsfiddle.net/XTm55/1/" rel="nofollow">forked version of your fiddle</a> showing how to accomplish what you're looking to do (I think) <em>(I've put the html/css/js at the bottom of this post as well)</em>.</p> <p>When the DOM content has loaded, the <code>alignItems</code> function makes sure that all images stay in the same place on the page. The script keeps track of the current image being displayed. Every time a different link is moused over, the image being displayed gets updated, such that the old current image gets its opacity set to 0 and the new current image gets its opacity set to 1. Then CSS transitions can be used to animate the opacity, which blows away <code>$.fn.animate</code>, but will only work on modern browsers (see <a href="http://caniuse.com/#search=css%20transition" rel="nofollow">caniuse</a>...motherf***ing IE -<em>_</em>- ). Code below:</p> <p><strong>html</strong></p> <pre><code>&lt;div class="menu"&gt; &lt;a href="#" data-image-id="1"&gt;link 1&lt;/a&gt; &lt;a href="#" data-image-id="2"&gt;link 2&lt;/a&gt; &lt;a href="#" data-image-id="3"&gt;link 3&lt;/a&gt; &lt;a href="#" data-image-id="4"&gt;link 4&lt;/a&gt; &lt;a href="#" data-image-id="5"&gt;link 5&lt;/a&gt; &lt;/div&gt; &lt;div id="container"&gt; &lt;img id="1" src="http://farm7.staticflickr.com/6092/6330704947_dd7e1b453c_t.jpg" /&gt; &lt;img id="2" src="http://farm1.staticflickr.com/13/15463218_8651d51b21_t.jpg"/&gt; &lt;img id="3" src="http://farm3.staticflickr.com/2570/4220856234_029e5b8348_t.jpg" /&gt; &lt;img id="4" src="http://farm4.staticflickr.com/3036/2975303180_86c4858b2b_t.jpg" /&gt; &lt;img id="5" src="http://farm7.staticflickr.com/6216/6240217938_aeed84634a_t.jpg" /&gt; &lt;/div&gt; </code></pre> <p><strong>css</strong></p> <pre><code>.menu a { padding: 2px 4px; color: #555; text-decoration: none; } .menu a:hover { color: #ddd; background: #333; } #container { margin-top: 10px; } #container img { margin: 4px; padding: 4px; border: 1px dashed #aaa; opacity: 0; -webkit-transition: opacity 500ms ease-in; -moz-transition: opacity 500ms ease-in; transition: opacity 500ms ease-in; } </code></pre> <p><strong>javascript</strong></p> <pre><code>$(document).ready(function () { alignImages(); var currentImgId = 0; $('.menu a').hover(function () { var oldImgId = currentImgId; currentImgId = this.dataset.imageId; $(document.getElementById(oldImgId)).css('opacity', 0); $(document.getElementById(currentImgId)).css('opacity', 1.0); }); }); function alignImages() { var $images = $('#container img'); var position = $images.eq(0).position(); $images.each(function() { this.style.position = 'absolute'; this.left = position.left; this.top = position.top; }); } </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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