Make text bigger  Make text smaller  Toggle background color  Bookmark/Share


MERCURY FREE WORDPRESS MAGAZINE THEME

Wordpress Mercury White Theme is an Adsense Ready, Widget ready, 3 mainstay Wordpress Theme, High Quality Design Theme for Wordpress, Beautiful Simple Gray as well as White wordpress thesis for Personal blogs, News Magazine Blogs or Neutral Blogs, Fixed width, Right sidebars as well as Web2.0 Interface, Compatible with Wordpress 2.x Versions…

Original post: 
Mercury Free Wordpress Magazine Theme


Get Auto Caffeinated Content for Your WordPress Blog



SHAZA

Le blog de shaza
 Shaza  Shaza  Shaza  Shaza

 Shaza

See a rest here:
Shaza


Get Auto Caffeinated Content for Your WordPress Blog



JENIFFER DAKE

Jeniffer Dake
 Jeniffer Dake  Jeniffer Dake  Jeniffer Dake  Jeniffer Dake

 Jeniffer Dake

Read a original: 
Jeniffer Dake


Get Auto Caffeinated Content for Your WordPress Blog



FREE WORDPRESS THEME : PINK BERRY DESIGN

Berry Cafe White Web2.0 Wordpress Theme Template, Very High Quality Design Theme for Wordpress Called Raspberry Cafe by Savvy, Come With Adsense-ready, widget-ready, WP 2.6 compatible, 2-column, natural, red, green, as well as white, Suitable Theme For Fruits as well as Food Blogs or Personal Pages Blogs, This work is underneath Creative Commons Attribution-Share Alike 3.0 License, This equates to we might have use of it as well as have review some-more

Read a strange post: 
Free Wordpress Theme : Pink Berry Design


Get Auto Caffeinated Content for Your WordPress Blog



RICARDO TERUEL

ricardo teruel blog
 Ricardo Teruel  Ricardo Teruel  Ricardo Teruel  Ricardo Teruel

 Ricardo Teruel

Read a strange post:
Ricardo Teruel


Get Auto Caffeinated Content for Your WordPress Blog



MAGNET

MAGNET
 Magnet  Magnet  Magnet  Magnet

 Magnet

Go here to see a original: 
Magnet


Get Auto Caffeinated Content for Your WordPress Blog



THE OTHER

The Other Wes Moore
 The Other  The Other  The Other  The Other

 The Other

See strange here:
The Other


Get Auto Caffeinated Content for Your WordPress Blog



HUGS

Hugs For Monsters
 Hugs  Hugs  Hugs  Hugs

 Hugs

Read more:
Hugs


Get Auto Caffeinated Content for Your WordPress Blog



ONE CHAPTER A DAY

One Chapter a Day
 One Chapter a Day  One Chapter a Day  One Chapter a Day  One Chapter a Day

 One Chapter a Day

View strange here: 
One Chapter a Day


Get Auto Caffeinated Content for Your WordPress Blog



30 PRO JQUERY TIPS, TRICKS AND STRATEGIES

jquery-banner 30 Pro jQuery Tips, Tricks and Strategies

Whether you’re a developer or a designer, a clever jQuery skillset is something we can’t equates to to be without. Today, I’m starting to uncover we thirty accessible jQuery coding tricks that will assistance we have your scripts some-more robust, superb as good as professional.

Getting Started

These tips as good as tricks all have a single thing in common- they have been all smashingly useful. With this things in your behind pocket, you’ll be ready to go shift a world, as good as even better, write jQuery identical to we know what you’re doing. It’s gonna be fun.

We’ll begin with a small elementary tricks, as good as pierce to a small some-more modernized things identical to essentially fluctuating jQuery’s methods as good as filters. Of course, we should be informed with a basis of jQuery first. If we haven’t used jQuery before, we rarely suggest browsing a documentation as good as examination jQuery for Absolute Beginners Video Series. Otherwise, you’re ready to puncture in!

#1 – Delay with Animate()

animate 30 Pro jQuery Tips, Tricks and Strategies

This is a unequivocally quick, easy approach to equates to behind actions in jQuery yet regulating setTimeout. The approach we have it work is to supplement an spur duty in to your sequence as good as spur a component to 100% opacity (which it’s already at), so it looks identical to zero is happening.

For instance, let’s contend that we longed for to open a dialog as good as afterwards blur it divided after 5 seconds. Using animate, we can do it identical to this:

1
2
3
$(function(){
	$("body").append("<div class='dialog'></div>").<em>animate({ opacity : 1.0 }, 5000)</em>.fadeOut();
});

Don’t we usually adore jQuery chaining? You’re acquire to review some-more about this technique from Karl Swedberg.

UPDATE: jQuery 1.4 has separated a need for this penetrate with a process called delay(). It is usually what is sounds identical to – a duty privately finished to check an existence effect. Way to go, jQuery!

#2 – Loop by Elements Backwards

One of my personal favorites is being equates to to double behind retrograde by a set of elements. We all know each() lets us simply double behind by elements, yet what if we need to go backwards? Here’s a trick:

1
2
3
4
5
6
7
8
$(function(){
	var reversedSet = $("li").get().reverse();
	//Use get() to lapse an form of elements, as good as afterwards retreat it
 
	$(reversedSet).each(function(){
		//Now we can block a topsy-turvy set right in to a any function. Could it be easier?
	});
});

#3 – Is There Anything in a jQuery Object?

Another unequivocally facile yet continually utilitarian pretence is checking if there have been any elements in a jQuery object. For example, let’s contend we need to find out if there have been any elements with a category of ‘active’ in a DOM. You can do that with a discerning check of a jQuery object’s length skill identical to this:

1
2
3
4
5
$(function(){
	if( $(".active").length ){
		//Now a formula here will usually be executed if there is at slightest a single active element
	}
});

This functions since 0 evaluates false, so a countenance usually evaluates loyal if there is at slightest a single component in a jQuery object. You can additionally have have have have have have have have make make make make make use of of of of of of of of of of of of of size() to do a same thing.

#4 – Access iFrame Elements

Iframes aren’t a most appropriate resolution to most problems, yet when we do need to have have have have have have have have make make make make make use of of of of of of of of of of of of of a single it’s unequivocally accessible to know how to entrance a elements inside it with Javascript. jQuery’s contents() process creates this a breeze, enabling us to bucket a iframe’s DOM in a single line identical to this:

1
2
3
4
5
6
7
$(function(){
	var iFrameDOM = $("iframe#someID").contents();
	//Now we can have have have have have have have have make make make make make use of of of of of of of of of of of of of <strong>find()</strong> to entrance any component in a iframe:
 
	iFrameDOM.find(".message").slideUp();
	//Slides up all elements classed 'message' in a iframe
});

#5 – Equal Height Columns

This was a single of CSS Newbie’s most renouned posts of 2009, as good as it is a good, plain pretence to have in your toolbox. The duty functions by usurpation a organisation of columns, measuring any a single to see that is largest, as good as afterwards resizing them all to compare a greatest one. Here’s a formula (slighly modified):

1
2
3
4
5
6
7
8
9
10
11
12
$(function(){
	jQuery.fn.equalHeight = function () {
		var tallest = 0;
		this.each(function() {
			tallest = ($(this).height() > tallest)? $(this).height() : tallest;
		});
		return this.height(tallest);
	}
 
	//Now we can call equalHeight
	$(".content-column").equalHeight();
});

An engaging as good as identical judgment is a overwhelming jQuery masonry plugin, if you’re meddlesome in checking it out.

#6 – Find a Selected Phrase as good as Manipulate It

Whether you’re seeking to perform find as good as replace, prominence poke terms, or something else, jQuery again creates it easy with html():

1
2
3
4
5
6
7
8
9
10
11
12
$(function(){
	//First conclude your poke string, deputy as good as context:
	var word = "your poke string";
	var deputy = "new string";
	var context = $(body);
 
	//
	context.html(
		context.html().replace('/'+phrase+'/gi', replacement);
	);
 
});

#7 – Hack Your Titles to Prevent Widows

Nobody likes to see widows – yet thankfully with a small jQuery as good as a small assistance from &nbsp; we can stop that from happening:

1
2
3
4
5
6
7
8
$(function(){
	//Loop by any title
	$("h3").each(function(){
		var calm = $(this).text().split(" ");
		var widow = "&amp;nbsp;"+content.pop();
		$(this).html(content.join(" ")+widow);
	});
});

This technique was referred to in a criticism by Bill Brown on Css-Tricks.

#8 – Add Pseudo-Selector Support in IE

Whether or not to await IE (especially 6) is a hotly debated issue, yet if we have been of a “let’s-make-the-best-of-this” camp, it’s good to know that we can supplement pseudo-selector await with jQuery. And we aren’t usually singular to :hover, nonetheless that’s a most common:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$(function(){
	<strong>//ADD HOVER SUPPORT:</strong>
	function hoverOn(){
		var currentClass = $(this).attr('class').split(' ')[0]; //Get initial category name
		$(this).addClass(currentClass + '-hover');
	}
	function hoverOff(){
		var currentClass = $(this).attr('class').split(' ')[0]; //Get initial category name
		$(this).removeClass(currentClass + '-hover');
	}
	$(".nav-item").hover(hoverOn,hoverOff);
 
	<strong>//ADD FIRST-CHILD SUPPORT:</strong>
	jQuery.fn.firstChild = function(){
		return this.each(function(){
			var currentClass = $(this).attr('class').split(' ')[0]; //Get initial category name
			$(this).children(":first").addClass(currentClass + '-first-child');
		});
	}
	$(".searchform").firstChild();
});

The good thing about environment it up that approach firstChild(), hoverOn() as good as hoverOff() have been unequivocally reusable. Now, in a CSS we can simply supplement a ‘nav-item-hover’ or ’searchform-first-child’ classes as a single some-more selectors:

1
2
3
4
5
6
7
8
.nav-item:hover, <strong>.nav-item-hover</strong>{
	background:#FFFFFF;
	border: solid 3px #888;
}
.searchform:first-child, <strong>.searchform-first-child</strong>{
	background:#FFFFFF;
	border: solid 3px #888;
}

It’s not pretty, yet it is current as good as it works. I’ve got to say, though, that we certain am seeking brazen to a day we won’t have to worry with this stuff!

#9 – Manage Search Box Values

A renouned outcome is to fill a site’s poke box with a worth (like ’search…’) as good as afterwards have have have have have have have have make make make make make use of of of of of of of of of of of of of jQuery to transparent a default worth when a margin receives focus, reverting if a margin is dull when blurred. That is simply achieved with a integrate lines of jQuery:

1
2
3
4
5
6
7
8
9
$(function(){
	//set default value:
	$("#searchbox")
	  .val('search?');
	  .focus(function(){this.val('')})
	  .blur(function(){
		(this.val() === '')? this.val('search?') : null;
	  });
});

#10 – Create a Disappearing ‘Back-to-Top’ Link

The disintegrating back-to-top integrate was desirous by Brian Cray. All we have to do is supplement a back-to-top integrate at a bottom of your calm identical to normal, as good as afterwards jQuery performs a magic:

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
$(function(){
/* set variables locally for increasing opening */
	var scroll_timer,
		displayed = false,
		$message = $('#message a'),
		$window = $(window),
		top = $(document.body).children(0).position().top;
 
	/* conflict to corkscrew eventuality on window */
	$window.scroll(function () {
		window.clearTimeout(scroll_timer);
		scroll_timer = window.setTimeout(function () { // have have have have have have have have make make make make make use of of of of of of of of of of of of of a timer for performance
			if($window.scrollTop() <= top) // censor if at a tip of a page
			{
				displayed = false;
				$message.fadeOut(500);
			}
			else if(displayed == false) // uncover if scrolling down
			{
				displayed = true;
				$message.stop(true, true).show().click(function () { $message.fadeOut(500); });
			}
		}, 100);
	});
});

Brian additionally combined a small nice-looking CSS, that we could supplement as a css record or conclude in an intent verbatim as good as ask it regulating jQuery.css(). Feel giveaway to go check out his in-depth explanation if we wish to sense more.

#11 – Easily Respond to Event Data

One of my a a single preferred things about jQuery is a available remapping of eventuality data, substantially expelling cross-browser inconsitencies as good as creation events most simpler to reply to. jQuery passes an eventuality parameter in to all bound/triggered functions, that is ordinarily called e:

1
2
3
4
5
6
7
8
9
10
11
12
$(function() {
	//We can get X/Y coordinates on click events:
	$("a").click(function(<em>e</em>){
		var clickX = e.pageX;
		var clickY = e.pageX;
	});
 
	//Or acknowledge that pass was pressed:
	$("window").keypress(function(<em>e</em>){
		var keyPressed = e.which;
	});
});

You can check out a jQuery docs on this a single to see all a possibilites, or perspective this keycode reference if you’d identical to to demeanour up a certain key’s impression code.

#12 – Encode HTML Entities

The initial place we saw this referred to was over at Debuggable, as good as we have to contend they unequivocally came up with something good here. The thought is to furnish a jQuery outcome identical to PHP’s htmlentities(). Check this out:

1
2
3
4
5
6
7
8
9
$(function(){
	var calm = $("#someElement").text();
	var text2 = "Some <code> & such to encode";
	//you can conclude a fibre or get a calm of an component or field
 
	var html = $(text).html();
	var html2 = $(text2).html();
	//Done - html as good as html2 right away reason a encoded values!
});

#13 – Friendly Text Resizing

Originally referred to at ShopDev, this is an glorious approach to embody a small user-centricity in your formula (allowing them to carry out a font-size):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$(function(){
  // Reset Font Size
  var originalFontSize = $('html').css('font-size');
    $(".resetFont").click(function(){
    $('html').css('font-size', originalFontSize);
  });
  // Increase Font Size
  $(".increaseFont").click(function(){
    var currentFontSize = $('html').css('font-size');
    var currentFontSizeNum = parseFloat(currentFontSize, 10);
    var newFontSize = currentFontSizeNum*1.2;
    $('html').css('font-size', newFontSize);
    return false;
  });
  // Decrease Font Size
  $(".decreaseFont").click(function(){
    var currentFontSize = $('html').css('font-size');
    var currentFontSizeNum = parseFloat(currentFontSize, 10);
    var newFontSize = currentFontSizeNum*0.8;
    $('html').css('font-size', newFontSize);
    return false;
  });
});

As we said, this is good pretence to know as good as adds a small of that energetic friendship that people suffer so much.

#14 – Open External Links in a New Window

This outmost links penetrate has been referred to prior to at Cats Who Code, as good as nonetheless unlawful it’s a good approach to open outmost links in brand brand brand brand brand brand new windows yet causing validation errors in XHTML 1.0 Strict.

1
2
3
4
5
$(function(){
	$('a[rel$='external']').click(function(){
		this.target = "_blank";
	});
});

This functions by grabbing all links with an outmost rel as good as adding a vacant target. Same result, it’s usually not hardcoded in to a site.

#15 – Gracefully Degrading AJAX Navigation

AJAX navigation is good – yet not for users as good as bots who can’t have have have have have have have have make make make make make use of of of of of of of of of of of of of it. The good headlines is, it’s probable to suggest approach links to your calm whilst still presenting AJAX functionality (to users who have that capability) by throwing links prior to they go anywhere, returning fake on them as good as loading a AJAX calm instead. It could demeanour identical to this:

1
2
3
4
5
6
$(function(){
	$("a").bind("click",function(){
		//Put your AJAX ask formula here
		return false;
	});
});

Of course, this is unequivocally basic, yet it’s an necessary facet to any AJAX navigation. You can additionally check out SpeckyBoy’s post on Easy-to-Use Free Ajax Navigation Solutions.

#16 – Create an Array of GET variables

Although this is not privately a jQuery trick, it’s utilitarian sufficient to be enclosed here. Using GET variables in Javascript formula doesn’t occur everyday, yet when it does you’ll wish to know a discerning as good as fit approach to review them. All we have to do is get document.location.search as good as do a small parsing on it:

1
2
3
4
5
6
7
8
9
10
 
var searchArray = document.location.search.substring(1).split("&");
//Take off a '?' as good as apart in to apart queries
 
//Now we'll double behind by searchArray as good as emanate an associative form (object literal) called GET
var GET = [];
for (searchTerm in searchArray){
	searchTerm.split("="); //Divide a searchTerm in to skill as good as value
	GET[searchTerm[0]] = searchTerm[1]; //Add skill as good as worth to a GET array
}

#17 – Partial Page Refresh Using load()

This glorious technique found at a Mediasoft Blog is approach cold as good as unequivocally accessible for formulating a continually updating dashboard/widget/etc. It functions by regulating jQuery.load() to perform a AJAX request:

1
2
3
4
5
$(document).ready(function() {
	setInterval(function() {
		$("#content").load(location.href+" #content>*","");
	}, 5000);
});

Voila! It functions – no iframes, meta refreshes or alternative such nonsense.

#18 – Skin with jQuery UI

If you’re starting to write any jQuery plugins (I goal we have already!), we should know that a good approach to supplement coherence as good as magnificence is to soak up jQueryUI theming classes in to any widgets/visible elements your plugin produces. The good thing about we do this is that it cuts or eliminates a css we have to yield with a plugin, as good as it adds a lot of customizability, as good (which is a single of a pass factors in a successful plugin).

And a tangible we do is as elementary as guidance how a classes work as good as attaching them to plugin elements. we can see this would be generally good with plugins identical to form beautifiers as good as print sliders, creation it easy to keep a unchanging demeanour via a website or app. You can check out a Theming Reference here.

#19 – Include Other Scripts

Stylesheet switchers have been zero new, yet adding alternative scripts with jQuery is something that is mostly overlooked. The worth is twofold:

  1. It creates it easy to have quiescent book loading.
  2. It additionally allows us to supplement scripts at runtime, that could be utilitarian in a total horde of situations.

It’s as easy as regulating append() to supplement a brand brand brand brand brand brand new book to a conduct of a document:

1
2
3
4
5
6
7
8
$(function(){
	$("head").append("<script type='text/javascript' src='somescript.js'></script>");
 
	//Or, loading usually when a delayed things is ready:
	$("img,form").load(function(){
		$("head").append("<script type='text/javascript' src='somescript.js'></script>");
	});
});

#20 – Use Body Classes for Easy Styling

Do we wish to save on formula as good as keep your styling in a css record where it should be? Body classes (another good ideas referred to by Karl Swedberg) concede we to do that. In short, we can have have have have have have have have make make make make make use of of of of of of of of of of of of of jQuery to supplement a ‘JS’ category to a physique element, that will capacitate we to set styles in your css that will usually be practical if Javascript is enabled. For example:

1
2
3
$(document).ready(function() {
	$("body").addClass("JS");
});

For Javascript users a physique right away has a JS class, so in a CSS we can supplement styles identical to this:

ul.navigation{
	display:block;
}
.JS ul.navigation{
	display:none;
}

This gives us a good approach to shift styles formed on possibly or not Javascript is supported/enabled, as good as we can still keep a styling in a CSS file. Another engaging associated have have have have have have have have make make make make make use of of of of of of of of of of of of of of this technique is to supplement browser classes to a body, enabling easy browser-specific styling. You can review some-more about that here.

#21 – Optimize Your Performance

Experienced coders don’t have clients wait for – they write formula that runs fast! There have been multiform ways we can have your formula run faster like:

  • Reference id’s rsther than than classes (id preference is local as good as thus quicker)
  • Use for instead of each()
  • Limit DOM strategy by adding elements in a single large cube rsther than than a single at a time
  • Take worth of event delegation
  • Link to Google’s jQuery duplicate rsther than than hosting your own – it’s faster as good as regularly up to date

Basically, it all boils down to not creation jQuery do any some-more work than it has to (and regulating local abilites at your convenience possible). Giulio Bai wrote an excellent post on jQuery perfomance, if you’d identical to to puncture in deeper.

#22 – Adapt Your Scripts to Work Cross-Browser – The Right Way

Thankfully, jQuery’s cross-browser harmony unequivocally cuts down a need for browser hacks. Sometimes, though, it is good to be equates to to get report about a client, as good as we can do that clean as good as unobtrusively with jQuery.support:

1
2
3
4
5
//Does this customer follow a W3C box model?
var boxModel = $.support.boxModel;
 
//Does this customer await 'opacity'?
var opacity = $.support.opacity;

It’s really improved make make make make make use of of of of of to have have have have have have have have make make make make make use of of of of of of of of of of of of of feature-detection rsther than than browser sniffing, as good as this is a unequivocally fit approach to do it. Read some-more about jQuery.support’s properties here.

#23 – Configure jQuery to be Compatible with Other Libraries

We all find ourselves in situations where mixed libraries have been needed, as good as since jQuery isn’t a usually living room that uses a $ alias, compatiblity issues infrequently cocktail up. Thankfully, this is easy to repair regulating jQuery’s noConflict(). You can even conclude a law alias to reinstate a $:

1
2
3
4
var $j = jQuery.noConflict();
 
//Now we can have have have have have have have have make make make make make use of of of of of of of of of of of of of '$j' usually identical to '$'
$j("div").hide();

An swap technique is to hang jQuery calls in an unknown duty as good as pass jQuery in as a parameter. Then we can have have have have have have have have make make make make make use of of of of of of of of of of of of of whatever alias we want, together with a ‘$’. This is generally utilitarian for plugin authoring:

1
2
3
4
5
(function($){
	$(document).ready(function(){
		//You can have have have have have have have have make make make make make use of of of of of of of of of of of of of normal jQuery syntax here
	});
})(jQuery);

#24 – Efficiently Store Element-Specific Information with data()

data() is substantially a single of a obtuse used jQuery methods, nonetheless it positively shouldn’t be. It allows us to attach/retrieve as most interpretation as we wish to DOM elements yet misusing attributes, as good as is generally utilitarian for some-more formidable scripts. For example, Stefan Petre’s Colorpicker plugin uses interpretation a lot since it’s tracking lots of fields as good as colors, converting rgb to hex, etc. Here’s have been a small examples of how data() works:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$(document).ready(function() {
	//Set standing to 'unsaved'
    $("button:first").data("status", "unsaved");
 
	//Retrieve status
	var buttonStatus = $("button:first").data("status");
 
	//Change status, this time defining an intent literal
	$("button:first").data("status", {saved : true, index : 1});
 
	//Retrieve standing of index property
	var buttonStatusIndex = $("button:first").data("status").index;
 
	//Remove standing data
	$("button:first").removeData("status");
});

I’m certain we can suppose a outrageous border of possibilities this presents. Again, it’s worth celebration of a mass a documentation if we haven’t used data() before.

#25 – Extend/Modify Existing jQuery Functions

Nobody says we can’t have have have have have have have have make make make make make use of of of of of of of of of of of of of a existent jQuery height as a springboard for brand brand brand brand brand brand new ideas – as good as most have finished usually that regulating extend(), an additional smashing jQuery method. The renouned Easing plugin, for example, adds a small existence accumulation by fluctuating a easing object, an already existent intent that is upheld to animate() as good as others:

1
2
3
4
5
6
7
8
9
10
11
12
13
jQuery.extend({
	easing: {
		easein: function(x, t, b, c, d) {
			return c*(t/=d)*t + b; // in
		},
		easeinout: function(x, t, b, c, d) {
			if (t < d/2) return 2*c*t*t/(d*d) + b;
			var ts = t - d/2;
			return -2*c*ts*ts/(d*d) + 2*c*ts/d + c/2 + b;
		},
		easeout: function(x, t, b, c, d) {
			return -c*t*t/(d*d) + 2*c*t/d + b;
		}...

By fluctuating as good as mending jQuery’s default functionality, we can open up a total brand brand brand brand brand brand new universe of cold possibilities.

#26 – Reverse Engineer before() as good as after()

I regularly appreciated how jQuery supposing append()/prepend() and appendTo()/prependTo(), enabling us to simply perform an attach in possibly direction. I’ve wished, though, that a identical capability was supposing with before() as good as after(). To shift that, we can simply supplement dual functions called putBefore() as good as putAfter() that will perform that purpose. Here’s how:

1
2
3
4
5
6
7
8
9
10
11
12
$(function(){
	jQuery.fn.putBefore = function(dest){
		return this.each(function(){
			$(dest).before($(this));
		});
	}
	jQuery.fn.putAfter = function(dest){
		return this.each(function(){
			$(dest).after($(this));
		});
	}
});

#27 – Add an isChildOf() Test

I’m certain we all have found ourselves in this incident – wanting to know if an component is a successor of an additional element. The good headlines is, with a single line of formula we can magnify jQuery to concede this:

1
2
3
4
5
6
7
8
$(function(){
	jQuery.fn.isChildOf = function(b){return (this.parents(b).length > 0);};
 
	//Now we can weigh identical to this:
	if ( $("li").isChildOf("ul") ){
		//Obviously, a li is a kid of a ul so this formula is executed
	}
});

Thanks to Dan Switzer II for this contribution!

#28 – Add Custom Selectors

This is an additional a single that has been talked about a lot in a growth community, so we might have this already figured out. If not, get ready since this will open a small total brand brand brand brand brand brand new windows for jQuery efficiency. The reduced story is, jQuery allows us to magnify a countenance object, that equates to we can supplement whatever law selectors we want. For example, contend we longed for to supplement a selector chronicle of a isChildOf() process we wrote earlier:

1
2
3
4
5
6
7
8
9
10
$(function(){
	jQuery.extend(jQuery.expr[':'], {
		'child-of' : function(a,b,c) {
			return (jQuery(a).parents(c[3]).length > 0);
		}
	});
 
	//'child-of' is right away a current selector:
	$("li:child-of(ul.test)").css("background","#000");
});

Debuggable has a good post on this a single as well, if you’d identical to to review some-more about how a parameters work, etc.

#29 – Smooth Scrolling Without Plugin

Karl Swedberg posted this a single a whilst behind on a Learning jQuery site, as good as it is really worth a look. Of course, there have been a integrate of plugins to get ahead this (and they have some-more features), yet we consider a genuine worth in this is a excercise of we do it yourself. Plus, demeanour at how little it is:

1
2
3
4
5
6
7
8
9
10
11
12
13
$(document).ready(function() {
  $('a[href*=#]').click(function() {
    if (location.pathname.replace(/^//,'') == this.pathname.replace(/^//,'')
      && location.hostname == this.hostname) {
       var $target = $(this.hash);
       $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
       if ($target.length) {
           $target.ScrollTo(400);
           return false;
       }
    };
  });
});

#30 – Add Tabs yet a Plugin

jQuery tabs have been mostly lonesome yet additionally mostly used, as good as identical to a scrolling pretence on top of it’s critical to know how to do these yet a plugin. The initial thing to do is write a markup, that should be ideally respectable in a deficiency of Javascript:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<div class="widget">
	<h3>Popular</h3>
	<ul>
		<li>Item #1</li>
		<li>Item #2</li>
		<li>Item #3</li>
	</ul>
</div>
<div class="widget">
	<h3>Recent</h3>
	<ul>
		<li>Item #1</li>
		<li>Item #2</li>
		<li>Item #3</li>
	</ul>
</div>

With a bit of styling, this would demeanour usually fine, so we know a jQuery is starting to reduce gracefully. Now we can write a code:

1
2
3
4
5
6
7
8
9
10
11
$(document).ready(function() {
	$("div.widget").hide().filter(":first").before("<ul class='tabs'></ul><div class='tab-content'></div>").add("div.widget").each(function(){
		$(this).find("ul").appendTo(".tab-content");
		$("ul.tabs").append("<li>" +$(this).find(":header:first").text()+ "</li>");
	});
	$("ul.tabs li").click(function(){
		$(this).addClass("active");
		$(".tab-content ul").slideUp().eq($("ul.tabs li").index(this)).slideDown();
	});
	$("ul.tabs li:first").click();
});

Of course, that’s usually a single approach to do it. If you’d identical to to see a small alternative approaches, see Extra Tuts’ jQuery Tabs Tutorials collection.

Wrapping Up

Thanks for reading, we goal you’ve enjoyed it! In box you’re all dismissed up on jQuery now, here have been a small links to some-more good tips n’ tricks collections:

As we can see, there have been gigantic opportunities for extraordinary creation with jQuery. So keep experimenting, keep perplexing things, keep thinking, “What if?” You could be a subsequent jQuery supergeek!

 30 Pro jQuery Tips, Tricks and Strategies

The rest is here: 
30 Pro jQuery Tips, Tricks as good as Strategies


Get Auto Caffeinated Content for Your WordPress Blog

Pages