David Slack - Web developer

jQuery - Use of siblings

How good are siblings? I've just got 390 lines of code down to 20! They're very good!!

I've been playing with jQuery for a website I'm doing and I'd had to move one element to another element and fade it in. I had to do this 94 times.

To do this I gave each element an ID and used that to move and fade in on a mouse over to another element. I was going to use vanilla javascript within php and loop all the elements.

Because I had jQuery on the page I desided to use that, but I used it in the same way I'd use vanilla javascript so move and fade each ID. Unfortunatley, this took 390 lines just for the javascript! To do it I did this for each element and I worked fine: 

{syntaxhighlighter brush: jscript;fontsize: 100; first-line: 1; }$("#idElementID-75").hover(function(){$("#idVideoElementID-75").fadeIn(1);},function(){$("#idVideoElementID-75").fadeOut(1);}); $("#idVideoElementID-75").position({my: "left top", at: "right top", of: "#idElementID-75"});{/syntaxhighlighter}

Today, I had a look at changing my script to use siblings in jQuery. I got it down to one script using classes and siblinds to do all the elements in one go like:

{syntaxhighlighter brush: jscript;fontsize: 100; first-line: 1; } $(document).ready(function() { /** * On every mouse over of every element we check if * it is in correct place (next to its element). If * it's not we move it to the correct place then we * fade it in. * On mouse out we fade it out. */ $("div.clElementContainer a.clElement").hover( function() { if($(this).siblings(".clVideoElement").css('left') == '0px') // If the left is 0 { // Move the element to the correct place $(this).siblings(".clVideoElement").position( { my: "left top", at: "right top", of: $(this) }); } $(this).siblings(".clVideoElement").fadeIn(1); // Fade in the element }, function() { $(this).siblings(".clVideoElement").fadeOut(1); // Fade out the element } ); });{/syntaxhighlighter}

Latest content

There are lots of developers out there with varying degrees of experience and expertise, some mid level, some junior and some senior. Some...
First thoughts are Symfony is ace!Only been using Symfony for 1 project but the amount of coding it has saved has been worth the time spent...
The Lost and Found website was built by myself (David Slack) and designed by Arm & Eye.The site was created to promote a bar and...

Social networks

Contact me here or catch me on one of
the social networks below


What I'm listening to

Off-site News

My blog

Wednesday, 2 August, 2017 - 06:56
There are lots of developers out there with varying degrees of experience and expertise, some mid level, some junior and some senior. Some developers...
Tuesday, 5 March, 2013 - 09:49
First thoughts are Symfony is ace!Only been using Symfony for 1 project but the amount of coding it has saved has been worth the time spent learning...
Monday, 3 December, 2012 - 10:07
As a web developer and web designer I need to take into account everything on a site from SEO, marketing, usability across devices, the look and feel...

Try this on your mobile

Use your QR reader to read this

You should be able to use this right from the screen

Latest tweets

Calendar

M T W T F S S
 
 
 
 
 
 
1
 
2
 
3
 
4
 
5
 
6
 
7
 
8
 
9
 
10
 
11
 
12
 
13
 
14
 
15
 
16
 
17
 
18
 
19
 
20
 
21
 
22
 
23
 
24
 
25
 
26
 
27
 
28
 
29
 
30
 
31
 
 
 
 
 
 

You are here

Back to top

Copyright David Slack - Web developer