Solved

jQuery:    imageresize()  { if ((contentwidth) < 700px ){ ...do something  } else {  ....do nothing

Posted on 2013-05-21
3
291 Views
Last Modified: 2013-05-26
Hi Experts

I'm trying to get a bunch of images in a <div> to resize when the screen width is less than  700px --I got it to work with one image, which you can see online: www.amazinggirl.com.au.
Now I'm trying to get all images to resize within a <div id="fluid-content">... I thought I might achieve this with   imageSRCArray = imageSRC.split('_');  wih two versions of all the images: "BlaBla_big.png"  and  "BlaBla_small.png"  trying to split the imageSRCArray[2]   but it's not working...

 $(document).ready(function() {  
 function imageresize() {  
            var contentwidth = $('#content').width();  
              if ((contentwidth) < '700'){  
                $('.fluidimage').attr(function(){
                imageSRC = $(this).find('img').attr('src');
                imageSRCArray = imageSRC.split('_');
                $(this).find('img').attr('src', 'img/big_' + imageSRCArray[2]);
            } else {function() {
                $(this).find('img').attr('src', 'img/small_' + imageSRCArray[2]);
             }  
            }  
  
 imageresize();//Triggers when document first loads      
  
 $(window).bind("resize", function(){//Adjusts image when browser resized  
   imageresize();  
   });
  });

Open in new window


Here's the HTML:
<div id="content">  
 <img class="fluidimage" src="img/big_logo.png"/> 
  
  <div style="width: 100%; margin: 10% 0 10% 1.5%">
      <img src="img/big_toolBox.png" alt="Click & Choose" style="float: left; margin-top: 5%"/>
      <img src="img/big_girl.png" alt="Click & Choose" style="float: right;"/> 
  </div>
</div>

Open in new window


Here's the CSS:
#container{  width :100%;height:100%;  }
.fluidimage {  width: 100%;  }

Open in new window


Here's the images folder:
img folder
0
Comment
Question by:georgehowell
  • 2
3 Comments
 
LVL 44

Assisted Solution

by:Rainer Jeschor
Rainer Jeschor earned 250 total points
ID: 39183894
Hi,
sorry have just limited time, but you should move your function OUTSIDE of the document.ready handler and just call it from there.
So structure would be:
function ImageResize ...
document.ready (
call imageresize
bind window event
)
HTH
Rainer
0
 

Accepted Solution

by:
georgehowell earned 0 total points
ID: 39183939
I FOUND IT!

$(document).ready(function() {  
 
 function imageresize() {  
  var contentwidth = $('#content').width();

  $('img').each(function( index ) {
        //document.write($(this).attr('src'));
        var imageSRCArray = $(this).attr('src').split('_');
        if ((contentwidth) < '700'){
              $(this).attr('src',imageSRCArray[0] + '_small.png');
        } else {  
              $(this).attr('src',imageSRCArray[0] + '_big.png');  
        }
  });
 }  

 imageresize();//Triggers when document first loads      
 
 $(window).bind("resize", function(){//Adjusts image when browser resized  
   imageresize();  
  });  
 });


thanks for your comment Rainer, but I got to talk to another friend and he suggested this and it works
0
 

Author Closing Comment

by:georgehowell
ID: 39197414
original code had syntax errors - solution came through another source
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question