Showing posts with label jquery. Show all posts
Showing posts with label jquery. Show all posts

Wednesday, 23 January 2013

Wait until all images have loaded then fade in - jQuery

I'll be showing you a very easy way of having all your images hidden, and then fading in nicely when they have loaded, which gives a nicer effect than seeing them trace down the page or depixelate into an image...

Place this in your head just before the closing tag.. 


<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(e) {
 $('img').css('opacity',0); // hide all images
});
$(window).load(function() { // we use $(window).load because unlike $(document).ready, it isn't triggered until the client has finished loading completely.
 $('img').animate({'opacity':1},800); // everything is loaded, fade images in
});
</script>

Thursday, 22 March 2012

Custom select dropdown with CSS and JS

Dropdowns have been a major pain in the backside in the past when it comes to styling of them, the default look doesn't always suite your website theme.


Customization is very limited, but it's not very difficult to change the style of the dropdown with CSS and javascript.


Lets turn this:






Into this:






Step 1
The HTML - This is simple, basically we have a div which holds the select element, it also holds the span element:


<div class="selection">
    <select class="selectbox" onchange="changeValue(this.value);">
        <option>test select</option>
        <option>test select 2</option>
        <option>test select 3</option>
        <option>test select 4</option>
        <option>test select 5</option>
    </select>
    <span id="selector">test select</span>
</div>


Step 2
The CSS - this tells the select element to become invisible, and tells the span what to look like and to sit below the selectbox.


<style>
.selection { position:relative; }

.selectbox,#selector {
 position:absolute;
 top:0;
 left:0;
 font-family:Arial, Helvetica, sans-serif;
 font-size:15px;
}

.selectbox {
 width:220px;
 padding:5px;
 height:30px;
 opacity:0;
 z-index:5;
}

#selector {
 background-image:url(dropdown.jpg);
 background-repeat:no-repeat;
 padding:5px 43px 5px 10px;
 overflow:hidden;
 width:162px;
 height:20px;
 color:#fff;
}
</style>


Perfect, as you can see from the CSS, the span with the id of #selector uses a background image called dropdown.jpg - this gives the coloured background and the arrow, you can make your own one. - this image is 215x30px if you change the size of it make sure you adjust the CSS accordingly, basically fiddle around with the #selector width and padding. You may also need to change the .selectbox width and padding if you experience difficulty clicking the dropdown once you've changed the size.


Step 3
The Javascript - this is just to update the span with the selected dropdown option.


You can do this with the jQuery library (if you already use it on your website), or using built in javascript, both work fine:


jQuery


<script>
function changeValue(val)
{
 $('#selector').html(val);
}
</script>

Javascript


<script>
function changeValue(val)
{
 document.getElementById('selector').innerHTML = val;
}
</script>


So there you have it, the complete code is below:


Complete Code


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Custom Select box</title>
<script>
function changeValue(val)
{
 document.getElementById('selector').innerHTML = val;
}
</script>
<style>
.selection { position:relative; }

.selectbox,#selector {
 position:absolute;
 top:0;
 left:0;
 font-family:Arial, Helvetica, sans-serif;
 font-size:15px;
}

.selectbox {
 width:220px;
 padding:5px;
 height:30px;
 opacity:0;
 z-index:5;
}

#selector {
 background-image:url(dropdown.jpg);
 background-repeat:no-repeat;
 padding:5px 43px 5px 10px;
 overflow:hidden;
 width:162px;
 height:20px;
 color:#fff;
}
</style>
</head>

<body>
<div class="selection">
    <select class="selectbox" onchange="changeValue(this.value);">
        <option>test select</option>
        <option>test select 2</option>
        <option>test select 3</option>
        <option>test select 4</option>
        <option>test select 5</option>
    </select>
    <span id="selector">test select</span>
</div>
</body>
</html>

Thursday, 26 May 2011

How to remove the fonts.com footer banner

If you use webfonts.fonts.com for their custom web fonts API, and like me, you don't like their default footer banner.
You've probably wanted to remove it and replace it with your own custom, much slicker banner which works nicely with your website.
I recently had this problem and decided to do something about it.

How they do it
The javascript file you include in the head of your html document loads their custom css and font, it then waits for 6 seconds to load the banner, this ensures all your other scripts have loaded, leaving nothing left but their banner to be placed neatly at the bottom of the page looking hideous.

I am going to give two examples here to removing this banner, the first will be using the jQuery library.

Using the jQuery library
Below is the complete code, it would be nice if you could leave my simple credits in there, Basically what happens is we count how many div tags have been loaded into the page and then we keep checking for another div to be added at the bottom of the page, once this happens we change the css attribute of display to none, which hides the banner.

/* http://dannyscodesnippets.blogspot.com 2011 removing webfonts.fonts.com banner */

/* when the document is ready start */
$(document).ready(function(){
   /* set variables */
 var count,newcount;
   /* count total divs loaded on the page */
  count = $('div').length;
 function checkwebFontDiv() {
    /* count total divs loaded on the page */
  newcount = $('div').length;
    /* if the total divs has increased by 1 then continue */
  if(newcount == count+1){
     /* hide the last div (the fonts.com banner div) */
   $('div:last').css("display","none");
    /* otherwise check again */
  } else {
   setTimeout(checkwebFontDiv,5); 
  }
 }
   /* start checking for the new div */
 checkwebFontDiv();
});

This is an example of how to implement this into your webpage.
First of all you need to download the jQuery library from here jquery.com (latest version).
Secondly you need to make sure you place this inside your head tags
<html>
<head>
<title>My test page hiding fonts.com banner</title>
<script src="javascript/jquery.1.5.1-min.js" type="text/javascript"></script>
<script type="text/javascript">
/* http://dannyscodesnippets.blogspot.com 2011 removing webfonts.fonts.com banner */

/* when the document is ready start */
$(document).ready(function(){
   /* set variables */
 var count,newcount;
   /* count total divs loaded on the page */
  count = $('div').length;
 function checkwebFontDiv() {
    /* count total divs loaded on the page */
  newcount = $('div').length;
    /* if the total divs has increased by 1 then continue */
  if(newcount == count+1){
     /* hide the last div (the fonts.com banner div) */
   $('div:last').css("display","none");
    /* otherwise check again */
  } else {
   setTimeout(checkwebFontDiv,5); 
  }
 }
   /* start checking for the new div */
 checkwebFontDiv();
});
</script>
<head>

and that's it.

 
Using javascript
Below is the complete code, it would be nice if you could leave my simple credits in there, Basically what happens is we count how many div tags have been loaded into the page and then we keep checking for another div to be added at the bottom of the page, once this happens we change the css attribute of display to none, which hides the banner. The difference between this and the example above is that you don't need the jQuery library loaded, you also need to place this at the bottom of your webpage, just before the closing body tag.

<script>
/* http://dannyscodesnippets.blogspot.com 2011 removing webfonts.fonts.com banner */

 var count,newcount,xDivs;
 xDivs =  document.getElementsByTagName('div');
   // count total divs loaded on the page
 count = xDivs.length;
 function checkwebFontDiv() {
    // count total divs loaded on the page
  newcount = xDivs.length;
    // if the total divs has increased by 1 then continue
  if(newcount == count+1){
     // hide the last div (the fonts.com banner div)
   xDivs[count].style.display='none';
    // otherwise check again
  } else {
   setTimeout(checkwebFontDiv,5); 
  }
 }
   // start checking for the new div
 checkwebFontDiv();

</script>
</body>
</html>

That's it!
Any questions please comment.