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!

Advertisements

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!

Important Browser Specific CSS Properties You Should Be Aware Of

There are many different browsers being used this day in age. For the most part, browsers render CSS the same but there are a few instances where certain browsers just do not recognize some properties. This is where Cross-Browser Compatibility testing comes into play.

Sometimes, there is an easy fix to make the styles on a website to look the same across browsers. Today, the most common browser according to W3Schools, the browsers used most often currently are listed in the chart below:

browserstats

For now, I will ignore Internet Explorer as I will talk about how to use special instances for IE in a separate post.

Firefox has a special property prefix in some instances, this prefix is: -moz-

Similar to Firefox, Safari and Google Chrome are similar as in they both use webkit. The prefix for webkit properties is: -webkit-

Opera has a special property prefix in some instances, this prefix is: -o-

Below are important CSS/CSS3 properties to keep in mind while doing cross browser compatibility. These are the most common properties that can be easily rendered across browsers using these prefixes:

Border Radius (CSS3)

.RoundedCorners{

border-radius: 5px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
-o-border-radius: 5px;

}

Border Radius can take one input for the radius measurement, or four inputs:

.RoundedCorners{

border-radius: 5px 5px 10px 10px;
-moz-border-radius: 5px 5px 10px 10px;
-webkit-border-radius: 5px 5px 10px 10px;
-o-border-radius: 5px 5px 10px 10px;

}

 

Box Shadow (CSS3)

.DivWithShadow{

box-shadow: 5px 5px 5px #333333;
-moz-box-shadow: 5px 5px 5px #333333;
-webkit-box-shadow: 5px 5px 5px #333333;
-o-box-shadow: 5px 5px 5px #333333;

}

Gradient (CSS3)

Below is a linear gradient from the top to the bottom:

.GradientDiv{

background: linear-gradient(red, blue);
background: -webkit-linear-gradient(red, blue);
background: -o-linear-gradient(red, blue);
background: -moz-linear-gradient(red, blue);

}

Below is a linear gradient from left to right:

.GradientDiv{

background: linear-gradient(left, red , blue); 
background: -o-linear-gradient(right, red, blue);
background: -moz-linear-gradient(right, red, blue);
background: -webkit-linear-gradient(to right, red , blue);

}

Below is a linear gradient that goes diagonally:

.GradientDiv{

background: linear-gradient(left top, red , blue);
background: -o-linear-gradient(bottom right, red, blue);
background: -moz-linear-gradient(bottom right, red, blue);
background: -webkit-linear-gradient(to bottom right, red , blue);

}

 

Animation (CSS3)

For this example we will do a simple CSS3 transition that changes the color of a link gradually from one color to another.

First we set the color of the link:

a:link{

color: #333333;

}

Then we set the hover with the transitions, time transitions should take place, and cross-browser properties:

a:hover {

 color: #FF0;
 transition: color 0.5s;
 -moz-transition-property: color;
 -webkit-transition-property: color;
 -o-transition-property: color;
 -moz-transition-duration: 0.5s;
 -webkit-transition-duration: 0.5s;
 -o-transition-duration: 0.5s;

}

 

Transform (CSS3)

For this example, we will use the transform property of rotate:

.TiltedDiv {

 transform: rotate(2deg);
 -moz-transform: rotate(2deg);
 -webkit-transform: rotate(2deg);

}

In the next post, I will explain how to detect, add custom CSS, or remove certain properties in Internet Explorer. Keep Coding!

Change Opacity of Background Only

With the emergence of CSS3, many developers have stumbled across the CSS style for changing the opacity:

.lowOpacityDiv {
    opacity: .5;
}

It is very easy to institute as it accepts any number between .1 and 1 from lowest opacity to highest opacity. The unfortunate flaw of this method however, is that any content inside your <div> with the class of lowOpacityDiv will also be a lower opacity.

In many cases this is a problem because we may want the have a semi-transparent background on our <div>, but still want the content to be legible and have a 100% opacity.

The old fix for this was to upload a small .png file that had the desired opacity and set it as the background for the selected <div>. But there are a couple ways to use CSS3 to achieve this without using any image files. The first syntax uses RGBA (Red, Green, Blue, Alpha). The mark-up is simple and straight forward:

.lowOpacityDiv {
    background-color: RGBA(12, 13, 134, .5)
}

The above will output the RGB color code that you selected, with an opacity, or alpha channel, of 50%. There are many ways to find RGB color codes using hosted color pickers on various websites. Some easy to use color pickers for RGB syntax are:

http://www.colorpicker.com/

http://www.rapidtables.com/web/color/RGB_Color.htm

The second way to create a low opacity background in a <div> while keeping your content at a higher opacity is to use the HSLA method (Hue, Saturation, Light, Alpha). This syntax is put in the CSS just like the RGBA syntax:

.lowOpacityDiv {
    background-color: HSLA(126, 13%, 24%, .5)
}

The above will output the HSL color code that you selected, with an opacity, or alpha channel, of 50%. There are many ways to find HSL color codes using hosted color pickers on various websites. Some easy to use color pickers for HSL colors are:

http://www.hslpicker.com/

http://www.workwithcolor.com/hsl-color-picker-01.htm

Benefit

Using the methods mentioned above as opposed to using an image to achieve the same effect will help to decrease the load time of your page and in turn, having more people stay on your website. The longer the load time, the larger change you have of driving traffic away so this is definitely a great method to adopt for rendering lowered opacity backgrounds.

Browser Compatibility

The browser support for using this syntax on your projects is relatively high with FireFox, Opera, Safari, Chrome and IE 9 and above supporting it.

Thank you for stopping by!

The Deterioration of Flash and the rise of HTML5 & CSS3

Adobe Flash based websites were all the rage a few years ago but with the release of HTML5 and CSS3, Flash is slowly becoming a web platform of the past. Although some websites still use some Flash based elements (less and less), for the most part, unless Adobe makes major changes, Flash will soon become a thing of the past. This is why:

  • Adobe and Apple have had major differences in the last few years. This has caused Apple to make it very hard for Adobe to have their Flash platform render on any Apple device. Although there are some third party companies who have made plugins to resolve this device rendering issue, many potential viewers are unable to access Flash content.

 

  • Websites made entirely in Flash may look really nice (or way overdone) with animations and flashy content but they do not work with the browser. This means that any Flash website that the viewer navigates through does not allow for the use of the browser back / forward buttons. This may not seem like a big deal because you have a navigation menu that has internal links to every page on the site but many viewers are used to interacting with the website through their browser window. If viewers cannot use the browser to navigate, many will become frustrated and not return to the website.

 

  • Flash content is not Search Engine Optimizable (SEO). This means that search engine spiders cannot crawl through Flash websites and appropriately place them in search engines. Any website that is Flash cannot be optimized to show up first in search engine results. This is a major issue someone is attempting to find a website and does not have a URL.

With HTML5 and CSS3, we are able to do almost anything that we were able to do in Flash (animations/transitions) using solely mark-up. This bypasses all of the problems that I mentioned about Flash above. The only issue that I know of using some advanced transition/experimental level effects is that browser support may vary, and many old versions of browsers do not support advanced features that are present in CSS3 and HTML5. In rare instances the use of Flash objects may have their place, but the era of websites being built in entirely Flash is coming to an end.

Just sharing an observation. Feel free to comment, I always enjoy others opinions and learning new things. Thanks for stopping by and good luck on your inter-web journeys!

CSS3 Rounded Corners without Images

CSS3 has opened up a lot of doors for the world of web design as many features have created easy to institute, quick to load design options available. One of the most used simple CSS3 feature is called border-radius. border-radius allows the web designer to add rounded corners to divs, images, buttons, etc. It is even possible to create circles using border-radius and because the shapes made are purely code and no images, they will not affect your websites load time.

To create equal rounded corners using border-radius, create a class in your CSS file and call it whatever you want. for this example, we will name our class roundedCorners:

.roundedCorners {
	border-radius: 10px;
        -moz-border-radius: 10px;
        -webkit-border-radius: 10px;
}

Just add the class name to your HTML in the desired div, image, button, etc. tag and that selected area should now have rounded corners applied to each corner.

<div class="roundedCorners"> CONTENT </div>

OR

<img src="#" class="roundedCorners" />

You can ensure that you support all browsers by adding the browser specific border-radius calls as well as just border-radius. This may be unnecessary as more and more modern browsers understand the generic call, but it is still good practice to add browser specific calls to ensure cross-browser compatibility.

We can adjust the border-radius on each corner specifically so we can create some distinct shapes by using the following:

.roundedCorners {
      border-top-left-radius: 10px 5px;
      border-bottom-right-radius: 10% 5%;
      border-top-right-radius: 10px;
}

As shown above, you can specify each corner individually very easily. If you input one value into the call for a corner of your image, div, etc. the it will be an even rounded edge.

If you apply two values to a specific corner, it will calculate the border radii of the horizontal and the vertical edge creating an uneven corner which could be a cool unexpected effect.

Values can be input in either % or px. border-radius will calculate both.

To create circles, just add a border radius of 50% to your selected div, image, etc. (just make sure that your selected area is a square to begin with, a rectangle with rounded corners will not become a circle.)

.roundedCorners {
      border-radius: 50%
}

If you follow these simple steps, you will be able to add this easy, fun, and modern effect to your websites. Just add the CSS to your CSS file and then make sure you call the corresponding class in the place you want the effect to appear in your HTML file.

Have fun creating, Thanks for stopping by!