10 jQuery snippets every designer should know

Go to Source

thumbnailjQuery is used on thousands upon thousands of web pages. It’s one of the most common libraries to insert into pages, and it makes DOM manipulation a snap.

Of course, part of jQuery’s popularity is its simplicity. It seems we can do almost anything we like with this powerful library.

For all the options open to us, there are some snippets we tend to come back to time and time again. Today I’d like to give you 10 snippets that everyone, newbies to gurus, will use time and time again.

1) Back to top button

// Back To Top
$('a.top').click(function(){
$('document.body').animate({scrollTop : 0},800);
return false;
});

//Create an anchor tag
<a class=”top” href=”#”>Back to top</a>

As you can see using the animate and scrollTop functions in jQuery we don’t need a plugin to create a simple scroll to top animation.

By changing the scrollTop value we can change where we want the scrollbar to land, in my case I used a value of 0 because I want it to go to the very top of our page, but if I wanted an offset of 100px I could just type 100px in the function.

So all we are really doing is animating the body of our document throughout the course of 800ms until it scrolls all the way to the top of the document.

 

2) Checking if images are loaded

$(‘img’).load(function() {
console.log(‘image load successful’);
});

Sometimes you need to check if your images are fully loaded in order to continue with your scripts, this three line jQuery snippet can do that for you easily.

You can also check if one particular image has loaded by replacing the img tag with an ID or class.

 

3) Fix broken images automatically

$('img').error(function(){
$(this).attr('src', ‘img/broken.png’);
});

Occasionally we have times when we have broken image links on our website and replacing them one by one isn’t easy, so adding this simple piece of code can save you a lot of headaches.

Even if you don’t have any broken links adding this doesn’t do any harm.

 

4) Toggle class on hover

$(‘.btn').hover(function(){
$(this).addClass(‘hover’);
}, function(){
$(this).removeClass(‘hover’);
}
);

We usually want to change the visual of a clickable element on our page when the user hovers over and this jQuery snippet does just that, it adds a class to your element when the user is hovering and when the user stops it removes the class, so all you need to do is add the necessary styles in your CSS file.

 

5) Disabling input fields

$('input[type="submit"]').attr("disabled", true);

On occasion you may want the submit button of a form or even one of its text inputs to be disabled until the user has performed a certain action (checking the “I’ve read the terms” checkbox for example) and this line of code accomplishes that; it adds the disabled attribute to your input so you can enable it when you want to.

To do that all you need to do is run the removeAttr function on the input with disabled as the parameter:

$('input[type="submit"]').removeAttr("disabled”);

 

6) Stop the loading of links

$(‘a.no-link').click(function(e){
e.preventDefault();
});

Sometimes we don’t want links to go to a certain page or even reload it, we want them to do something else like trigger some other script and in that case this piece of code will do the trick of preventing the default action.

 

7) Toggle fade/slide

// Fade
$( “.btn" ).click(function() {
$( “.element" ).fadeToggle("slow");
});

// Toggle
$( “.btn" ).click(function() {
$( “.element" ).slideToggle("slow");
});

Slides and Fades are something we use plenty in our animations using jQuery, sometimes we just want to show an element when we click something and for that the fadeIn and slideDown methods are perfect, but if we want that element to appear on the first click and then disappear on the second this piece of code will work just fine.

 

8) Simple accordion

// Close all Panels
$('#accordion').find(‘.content').hide();
// Accordion
$('#accordion').find(‘.accordion-header').click(function(){
var next = $(this).next();
next.slideToggle('fast’);
$(‘.content’).not(next).slideUp('fast’);
return false;
});

By adding this script all you really need to on your page is the necessary HTML go get this working.

As you can see in this snippet I firstly closed all the panels in our accordion and then on the click event I made the content that is linked to that header slide toggle , and all the other ones slide up. It’s a simple method for a quick accordion.

 

9) Make two divs the same height

$(‘.div').css('min-height', $(‘.main-div').height());

Sometimes you want two divs to have the same height no matter what content they have in them, this little snippet enables just that; in this case it sets the min-height which means that it can be bigger than the main div but never smaller. This is great for masonry like websites.

 

10) Zebra stripped unordered list

$('li:odd').css('background', '#E8E8E8’);

With this little snippet you can easily create zebra striped unordered lists, this places the background you define on every odd list item so that you can place the default one for the even ones in your CSS file. You can add this snippet to any type of markup, from tables to plain divs, anything you want to be zebra stripped.

 

Conclusion

These are the pieces of jQuery code I find myself using again and again in my projects. I hope you bookmark this page and come back whenever you need one of these snippets.

 

What jQuery snippets do you rely upon? Do you have better code for these scenarios? Let us know in the comments.

Featured image/thumbnail, useful image via Shutterstock.

7 Breathtaking Ornamental Font Sets With 500+ Ornaments – only $12!
10 jQuery snippets every designer should know

Source

Dot Navigation Styles

Go to CODROPS [source]</a

DotNavigationStyles

View demo Download source

Small UI elements usually don’t get too much attention when it comes to creative effects. They are often neglected because of their size. But they can offer a great opportunity to add some subtle, yet interesting effects. Recently, you might have seen some kind of dot navigation with a vertical or horizontal layout for scrolling a website to a section. Today we want to share a set of inspirational dot navigation styles with very subtle effects when we hover or when we click on them. For the effects we employ several techniques, including transitions on pseudo-elements, perspective and SVG.

Note that some effects might not work as intended in some browsers (SVG transition, 3D transform-style). For older browsers you might have to add a simple fallback for the selected dot in some examples.

For the structure we’ll have an unordered list with links:

<div class="dotstyle dotstyle-fillup">
	<ul>
		<li class="current"><a href="#">Home</a></li>
		<li><a href="#">About</a></li>
		<li><a href="#">Products</a></li>
		<li><a href="#">Portfolio</a></li>
		<li><a href="#">Blog</a></li>
		<li><a href="#">Contact</a></li>
	</ul>
</div>

For some examples, we also use an additional empty list element, i.e. in order to “move” the current item in the “dot move” style.

We define some common styles:

.dotstyle ul {
	position: relative;
	display: inline-block;
	margin: 0;
	padding: 0;
	list-style: none;
	cursor: default;
}

.dotstyle li {
	position: relative;
	display: block;
	float: left;
	margin: 0 16px;
	width: 16px;
	height: 16px;
	cursor: pointer;
}

.dotstyle li a {
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
	outline: none;
	border-radius: 50%;
	background-color: #fff;
	background-color: rgba(255,255,255,0.3);
	text-indent: -999em;
	cursor: pointer; /* make the text accessible to screen readers */
	position: absolute;
}

An example for a style and subtle effect is the following one, where we “fill” the inner circle. We do that by setting the link to overflow hidden and using a pseudo-element as the filling part:

/* Fill up */
.dotstyle-fillup li a {
	overflow: hidden;
	background-color: rgba(0,0,0,0);
	box-shadow: inset 0 0 0 2px rgba(255,255,255,1);
	transition: background 0.3s;
}

.dotstyle-fillup li a::after {
	content: '';
	position: absolute;
	bottom: 0;
	height: 0;
	left: 0;
	width: 100%;
	background-color: #fff;
	box-shadow: 0 0 1px #fff;
	transition: height 0.3s;
}

.dotstyle-fillup li a:hover,
.dotstyle-fillup li a:focus {
	background-color: rgba(0,0,0,0.2);
}

.dotstyle-fillup li.current a::after {
	height: 100%;
}

Another interesting example is the stroke that we draw using SVG and transitioning the stroke-dashoffset of the circle shape:

/* SVG draw circle stroke */	
.dotstyle-drawcircle li {
	width: 18px;
	height: 18px;
}

.dotstyle-drawcircle li a {
	top: 3px;
	left: 3px;
	width: 12px;
	height: 12px;
	background-color: #c44d48;
	-webkit-transition: opacity 0.3s;
	transition: opacity 0.3s;
}

.dotstyle-drawcircle li svg {
	z-index: 10;
}

.dotstyle-drawcircle li svg circle {
	opacity: 0;
	fill: none;
	stroke: #fff;
	stroke-width: 3;
	stroke-linecap: round;
	stroke-linejoin: round;
	stroke-dasharray: 39 39; 
	stroke-dashoffset: 39; /* ~ length of circle path (pi*2r) */
	transition: stroke-dashoffset 0.3s, opacity 0.3s;
}

.dotstyle-drawcircle li.current a,
.dotstyle-drawcircle li a:hover,
.dotstyle-drawcircle li a:focus {
	opacity: 0.5;
}

.dotstyle-drawcircle li.current svg circle {
    opacity: 1;
    stroke-dashoffset: 0;
	transition: stroke-dashoffset 0.3s, opacity 0.15s;
}

For the “hop” effect we also use an extra bit of JavaScript in order to add another class to the current item which indicates if the previous current item was on the right. This will allow us to change the transform origin and let the “hop” happen from the right.

Hope you enjoy these styles and find them inspiring!

View demo Download source

Giveaway: Win 1 of 10 professional WordPress themes from TemplateMonster

Go to Source

tm_thumbnailA great source for high-quality WordPress themes is TemplateMonster, so we’re absolutely stoked to be able to announce this awesome giveaway.

What better way to start 2014 than with a brand new look courtesy of TemplateMonster?

10 lucky winners will each be able to choose 1 theme, from TemplateMonster’s collection of over 2000 themes. With that many options, you know you’ll find something to fit the bill.

The choice of which template to select is entirely down to you (if you’re one of the lucky winners) but we recommend that you choose one of the 500+ themes based on TemplateMonster’s popular CherryFramework.

The CherryFramework is built on the hugely successful Bootstrap framework, making it fully responsive, so you’ll look great no matter what device your customers are using. Installation of the themes is incredibly simple, and once you’ve got them installed there are over 50 different shortcodes to speed up your productivity. The CherryFramework also features flexible post formats.

The CherryFramework themes even support the WordPress Multilingual Plugin, making them an ideal choice for anyone running a business with an international clientele.

Most importantly, TemplateMonster have built the CherryFramework on a parent > child basis, meaning that they can update the framework with every new version of WordPress without overwriting any changes you’ve made to the child theme.

Just take a look at a small sample of some of the 2000+ WordPress themes you could be choosing from:

Giveaway: Win 1 of 10 professional WordPress themes from TemplateMonster

Giveaway: Win 1 of 10 professional WordPress themes from TemplateMonster

Giveaway: Win 1 of 10 professional WordPress themes from TemplateMonster

Giveaway: Win 1 of 10 professional WordPress themes from TemplateMonster

Giveaway: Win 1 of 10 professional WordPress themes from TemplateMonster

Giveaway: Win 1 of 10 professional WordPress themes from TemplateMonster

Giveaway: Win 1 of 10 professional WordPress themes from TemplateMonster

Giveaway: Win 1 of 10 professional WordPress themes from TemplateMonster

 

How to enter:

  • Browse through TemplateMonster’s themes gallery, then post a comment letting us know which theme you’d like to win and why;
  • comment MUST be entered with your email address as a guest (no social logins);
  • one comment per person;
  • winners will be contacted via email.
  • closing date for entries is January 28th 2014, good luck!

 

Have you used TemplateMonster themes? Which theme would you choose if you’re one of the winners? Let us know in the comments.

7 Breathtaking Ornamental Font Sets With 500+ Ornaments – only $12!
Giveaway: Win 1 of 10 professional WordPress themes from TemplateMonster

Source