It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming JavaScript Forum Script-archive

Changing Objects Via TagName & ClassName

Changing Objects Via TagName & ClassName

Postby HotNoob on Thu Sep 23, 2010 2:48 pm

Here is something that i found quite useful for images, especially when the host has external image fetching disabled in php.

Code: Select all
<script>
//this function fetches all tags with your desired class name, and resizes the object to fit within the defined parameters.
function sizeByTagClass(tagType,className, maxWidth, maxHeight)
  {
    a = document.getElementsByTagName(tagType)||document.getElementsByClass(className);
    for (i=0;i<a.length;i++)
    {
        if(a[i].className == className)
        {
          if(a[i].height >= maxHeight)
          {
             var scale = maxHeight/a[i].height;
              a[i].height *= scale;
          }
          if(a[i].width >= maxWidth)
          {
              var scale = maxWidth/a[i].width;
              a[i].height *= scale;
          }
        }
    }
  }
</script>


Here is an example on how to use this function
Code: Select all
<script>
sizeByTagClass('img','smImg', 385, 385);
function to()
{
  sizeByTagClass('img','smImg', 385, 385);
}
var g_tempInt = setInterval('to()', 2000); //this is for browsers that may struggle with this function.
</script>

Basically, it is using JavaScript to do CSS work, but with a lot more flexibility :D
HotNoob
100+ Club
 
Posts: 169
Joined: Sun May 02, 2010 1:38 am

Who is online

Users browsing this forum: No registered users and 0 guests