Fade Divs In and Out with jQuery

DivSwitch

Switching content areas without changing the page is a feature that is appearing more and more in modern web design. On some websites, this is used as the main focal point of the user interface, and on other sites, it helps to minimize the amount of area that is taken up by info and consolidates multiple areas of information into one location enhancing the user experience.  This is achievable many different ways, one of the simplest ways using jQuery.

There is a working demo available here. (JSfiddle)

In order to accomplish this task, We must include our jQuery file in between the head tags:

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
</head>       

or at the bottom of your body:

<body>

...BODY CONTENT

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
</body>

as shown above.

Then we can begin coding the jQuery that will switch between our divs fading them in and out on the button click.

<script type="text/javascript">

$(document).ready(function () {
    $('.BTN').click(function () {
        $('.div').fadeOut('slow', function () {
            $('.div2').fadeIn('slow'); 
        });

        $('.BTN2').click(function () {
            $('.div2').fadeOut('slow', function () {
                $('.div').fadeIn('slow'); 
            });
        });
    });
});

</script>

At first glance, the above script may appear confusing but when broken down it is actually very simple. We call up the jQuery file:

$(document).ready(function () {

Then we focus on our first button, give the button a function (click();) then tell the function to fade the section with a class of .div out and the section with a class of .div2 in.

 $('.BTN').click(function () {
        $('.div').fadeOut('slow', function () {
            $('.div2').fadeIn('slow'); 
        });

After that, we focus on the second button, give the button the click(); function, and tell the function to fade the section with a class of .div2 out and the section with a class of .div1 in.

$('.BTN2').click(function () {
            $('.div2').fadeOut('slow', function () {
                $('.div').fadeIn('slow'); 
            });
        });

Once this is complete, we have created our fading divs that switch when a button is clicked. Now just style your page and you’ve created two sections that switch back and forth between eachother.

I hope you found this post helpful! Good luck coding the web!

Simple Age Verification with jQuery

verificationSS

Today I will go over a simple age verification that I built during my downtime this morning. It uses basic jQuery functions to select, based on the users answer, whether they are of age to view the content of a website or not. This may be used in other ways as well in order to block content before the user decides to view it.

This is a common function on the web today so I decided to show a very easy way to accomplish the task.

I built this project online, and it can be viewed here. (jsfiddle)

I styled the page using HTML and CSS3.

Note: The styling was done without using tables as they are outdated and should rarely be used in modern web design.

In order to accomplish this task, We must include our jQuery file in between the head tags:

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
</head>       

or at the bottom of your body:

<body>

...BODY CONTENT

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
</body>

as shown above.

Then we write the jquery that will reference the linked document and give us the desired output. before we start coding the functionality, we  need to set our variables so the jQuery knows what we mean when we refer to “Denied” and “Welcome”

var Denied = "Access is denied.";
var Welcome = "Welcome! You are authorized to view age restricted content";

<script type="text/javascript">
$(document).ready(function () {
      $(".Yes").click(function () {
      if (alert(Welcome)) {
      } else {
        window.location.href ="http://www.jhauge.com"
        return false;
        }
    });

    $(".No").click(function () {
      if (alert(Denied)) {
      } else {
        window.location.href ="http://www.google.com"
        return false;
        }
    });

});
</script>

The script above is what the complete jQuery script looks like. Now I will break it down to explain the functionality.

$(document).ready(function () {
SCRIPT IN HERE
});

First we reference the attached jquery file letting it know we are going to be calling a function. Then we reference our div with the class of .Yes (letting the jquery know what to perform the function on) and then we call the function we want to use, in this case it’s .click().

 $(".Yes").click(function () {
      if (alert(Welcome)) {
      } else {
        window.location.href ="http://www.LINK.com"
        return false;
        }

After the function is called, the next step is to perform an action when that function is completed. For our purposes, we have an alert box pop up when yes is selected by the user telling them that access has been granted, and it forwards them to the page that was hidden using the function window.location.href .

The same was done for the div with the class of .No in order to make the no button functional. The difference is that if no is clicked, the user is directed to a search engine instead of to the protected content, leading those who are underage away from the protected content.

I hope you found this tutorial useful. Thanks for stopping by!

Cool, Easily Customizable Map Resources

Lately on many B2B websites and company websites that have been redesigned within the past year or so, there has been a rash of large map areas that are in custom colors to match the branding of the website. These maps may be used next to contact information or even as a background on the contact page itself. This is a really cool idea because it really makes the contact information stand out and helps someone who is looking for directions to find you quickly and easily.

 

The first one I am going to go over is called MapBox. http://www.mapbox.com

MapBox is a website that allows you to completely customize maps to exactly the colors and styles you want. They have premium plans and free ones, and they offer mapbox.js, and some API’s for developers so you can insert your map directly into your website. I am not entirely sure of the range of use of this program, but it is definitely easier to implement than taking a Google Map and altering it to your desired colors and exactly your specifications.  I have not used it (as of yet) but i plan on trying it out as soon as I get a project that it will work within!

*Please note that this platform does not use Google Maps service, so there may be variations from a map through MapBox and a map through Google Maps.

 

Another custom map website that I found while perusing the internet is SnazzyMaps http://snazzymaps.com/

SnazzyMaps is a repository for Google Maps that have already been modified to different color schemes. Check through the large library of pre-customized maps and if any work for you, they are easily implemented using Google Maps API and the code snippets that are found with each color theme. IF you want to include a custom Google Mapp on your website, this is definitely a great place to look to cut off some time in production!

Customize Font on the Web using Free Google Webfonts

One of the things that you may be looking to do is add a particular style font face to your website to complete the look and feel you are designing for. If none of the fonts that are preloaded into the browser seem to complete the look, there is a very simple API that has been developed by Google that allows you to choose from hundreds of different fonts to use in your website.

There are other sources that have similar API’s but Google will most likely stay around for the duration that you are using their hosted files while I cannot say the same about any other company as easily.

In order to include a font from Google’s web font library, go to http://www.google.com/fonts. Choose a font that you want to use and click the Quick-Use button in the bottom right hand corner of the chosen fonts section on the page. Scroll to the bottom of the page and copy the

<link href='http://fonts.googleapis.com/css?family=Orbitron:400,500' rel='stylesheet' type='text/css' />

and paste it into the head of your document between your <head> </head> tags.

This tells your browser to load that font from the Google web fonts library. Now you just need to tell the browser where to use the font in your page. We do this in our CSS file. We can put it in our body tag, p tag, h1 tag, etc.

body {
    font-family:'Orbitron', sans-serif;
    font-weight: 400;
}

You can copy and paste this code directly from the font’s page on the Google API website as well.

This will allow you to complete the feel of your website with custom typography.

Animate with jQuery: Create a Heartbeat/Pulsing Effect on Text or Links

Within the jQuery library, there are thousands upon thousands of different effects that can be applied to your page to create a unique and dynamic user experience. Animation using mark-up has become more and more prevalent as of late, since animation that uses flash is losing its relevance and its place in modern web (An explanation that I will save for another post).

jQuery is a library that we have used before to create a smooth scroll effect so we have already learned how to call a jQuery library into your page. If you do not remember, Google has uploaded and hosts versions of jQuery right on their website.

We will start off by linking to the jQuery library version 1.9.1 . This goes in the <head></head> tags in your HTML mark-up.

<head>
<title> PAGE TITLE </title>   
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>

Now that we have linked the correct version of the jQuery library to our HTML page, we now need to add jQuery to our page so that we can create the animation. I will break the script we will use down into pieces and explain the parts of the script and what they do.

** The script can be put in the <head></head> tags under your jQuery library call, but for a faster page load time, I suggest adding it to the very bottom of your HTML page, under your content right before you close your <body></body> tag.

So we start by opening our <script type=”text/javascript”> tag and telling the page to look at the jQuery library and call/set our function:

 <script type="text/javascript">
        $(document).ready(function animateHeart() {

We then set the class (for this example we will set our class as heart) we will use to identify the p, span, h1, a, etc. that will use the function and we set our class to the jQuery function .animate :

 $('.heart').animate({

Lastly, we need to set the font size we will start off with, and the size we wish to have our text shrink to and repeat the size we start with so that we get a bouncing or pulsating effect. We set our animation speed, and apply our function so the effect will act the way we set it. Dont forget to close your </script> tag at the end!

fontSize: $('.heart').css('fontSize') == '60px' ? '55px' : '60px'
            }, 500, animateHeart);
        });
        </script>

All together, the script appears as:

<script type="text/javascript">
        $(document).ready(function animateHeart() {
            $('.heart').animate({
                fontSize: $('.heart').css('fontSize') == '60px' ? '55px' : '60px'
            }, 500, animateHeart);
        });
        </script>

In order for this effect to work, make sure you dont forget to apply the class to a section in your HTML!

<a href="#about" class="heart">V</a>

Using this bit of jQuery, you can help improve the UX on your website and add pull the users eye to information that may be of importance or to links they should be clicking on.

Thanks for stopping by, as always. Good Luck and have fun developing!

Multiple Link Styles in one HTML page

When building a webpage, stating link styles is always one of the first steps that one takes to create a feel for the webpage. Without link styles set, the default style will just be blue underlined text. Link styles are fairly simple to set up in the CSS file. They appear like this:

a:link {
    color:#000000;
} 
a:visited {
    color:#000000;
}
a:hover {
    color:#FFFFFF;
}
a:active {
    color:#000000;
}

Now I will quickly explain the link states:

a:link = Setting the style for any unclicked link on our page. Links are called using an <a href=” URL “> URL TEXT </a> tag so the a we use in our CSS corresponds to our a tag in the HTML mark-up.

a:visited = Sets the link stste for the links once they have already been clicked on.

a:hover = Sets the link state when the mouse hovers over the link.

a:active = Sets the link state for links that are being clicked while they are being clicked.

This will set the style for all links on a page that the CSS file is linked to. Sometimes it is necessary to have different styles applied to links for readability or layout purposes. All we have to do is add a class to our link styles. By doing this, we can have as many different style links on our page that we want, we just need to make sure that we add the corresponding class to the link we want the style applied to.

Here is an example:

/* LINK STYLE 1 */

a.link1:link {
    color:#000000;
} 
a.link1:visited {
    color:#000000;
}
a.link1:hover {
    color:#FFFFFF;
}
a.link1:active {
    color:#000000;
}
/* LINK STYLE 2 */

a.link2:link {
    color:#FFFFFF;
    text-decoration: none;
} 
a.link2:visited {
    color:#FFFFFF;
    text-decoration: none;
}
a.link2:hover {
    color:#000000;
    text-decoration: none;
}
a.link2:active {
    color:#FFFFFF;
    text-decoration: none;
}

You can use any color that you would like they can all be the same, or they can be different. I would suggest making at the least the hover state different in some way, either color, text-decoration, positioning, etc. in order to create interactivity with links in your site. If the link does not interact, then the user may not know that they are hovering over a link.

Just make sure that you apply your link class to the HTML that you want to have the specified styles:

 <a href="#" class="link1"> LINK TEXT </a>

Above will have the link1 CSS styles applied to it, below will have the link2 styles applied to it.

 <a href="#" class="link2"> LINK TEXT </a>

I hope this was a help to you in your web development ventures. Thanks for stopping by!

Simple ‘Smooth Scroll’ with jQuery

Have you ever been to a website, clicked on an internal link (link on a page that goes to a different location on the same page), and the page jumped to the location?

A much cleaner, more captivating way to go about internal links is creating a smooth scroll effect so when the link is clicked, the page slides to the specified content instead of jumping to it. This seems at first glance to be complicated, but in reality, it is a very simple technique and a good way to push your design from basic and static content to a place where you can begin to enhance the user experience (UX).

To view this effect in action, visit jhauge.com and click on the ‘Let’s Talk’ link!

The first step we need to take is calling the correct version of jQuery into our page. We are going to use a google .api as google has hosted jQuery pages on their site. This is very easy to do:

<head>
<!-- HEADER CONTENT GOES HERE -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

</head>

A call to jQuery can go in between your < head > < /head > tags or at the bottom of your < body > tag.  For this specific install, we will be putting it in the < head > tags as this effect does not include much script to implement.

Underneath our call for google’s jQuery .api, we will write some custom script to tell our page what part of the linked script we need to use. In order to write script on a HTML page, you must wrap your custom code in <script> or even better <script type=”text/javascript”> tags.

We start by calling our function and giving our function a class. The class is applied to links that we want to perform the effect as I will demonstrate later:

<script type="text/javascript">
      $(document).ready(function(){
        $(".scroll").click(function(event){
         event.preventDefault();

We then calculate the browser size and the proximity to the destination specified in each link using and if/else statement:

var dest=0;
         if($(this.hash).offset().top > $(document).height()-$(window).height()){
              dest=$(document).height()-$(window).height();
         }else{
              dest=$(this.hash).offset().top;
         }

Lastly, we set the jQuery effect and the speed at which we want the scroll to move and then we close our script tag.

$('html,body').animate({scrollTop:dest}, 1000,'swing');
     });
     });
    </script>

The code in your header when complete should appear like this:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
     <script>
      $(document).ready(function(){
        $(".scroll").click(function(event){
         event.preventDefault();
         var dest=0;
         if($(this.hash).offset().top > $(document).height()-$(window).height()){
              dest=$(document).height()-$(window).height();
         }else{
              dest=$(this.hash).offset().top;
         }
         $('html,body').animate({scrollTop:dest}, 1000,'swing');
     });
     });
    </script>

Our last step is to link to an internal link in our HTML. In order to make an internal link, we set a specific ID to a <div>, <h1>, <p>, basically any tag that you would like to appear at the top of the browser screen when the link is clicked. This looks like this:

<div ID="blue">

Then, we set a link that goes to that ID by using a poundsign (#) which symbolizes a CSS ID, followed by the ID name like this:

<a href="#blue"> I am link text"</a>

This link when clicked, will now jump to the location specified by the selected ID. The only other thing we need to do in order to get the jQuery to be called and the page to smooth scroll to the link location is to call the class in the link tag. Your ending HTML should look like this:

<a href="#blue" class="scroll"> I am link text"</a>

Now when you click your link, your page should scroll smoothly to the specified location on your page.

This effect will enhance your page and help you to begin designing with the user experience in mind instead of just as a static web designer. Thanks for stopping by!