Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Growing Image

  1. #1
    Join Date
    Jul 2007
    Posts
    35
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Growing Image

    I came across this the other day: http://www.prlog.org/10136911-free-a...-you-want.html.

    Place the cursor on the image and it is enlarged (the image, not the cursor).

    Anyone know if this is easy to do or not?

    Thanks.
    Last edited by simonjones; 12-14-2008 at 02:18 PM.

  2. #2
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    2,023
    Thanks
    17
    Thanked 319 Times in 318 Posts
    Blog Entries
    3

    Default

    jQuery is great with this and it's quite easy to do.

  3. The Following User Says Thank You to Snookerman For This Useful Post:

    simonjones (12-13-2008)

  4. #3
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    When using WebKit browsers, you can actually do that using CSS alone. To dream of the day when that's supported in all browsers.

    I would second Snookerman's suggest of jQuery. You'll specifically want to look into the animate and CSS methods.

  5. The Following User Says Thank You to Medyman For This Useful Post:

    simonjones (12-13-2008)

  6. #4
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    2,023
    Thanks
    17
    Thanked 319 Times in 318 Posts
    Blog Entries
    3

    Default

    Here is what the code could look like:
    HTML 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>jQuery</title>
    <script type="text/javascript" src="jquery-1.2.6.min.js"></script>
    <style type="text/css">
    img {
    	width:320px;
    	height:240px;
    }
    </style>
    <script type="text/javascript">
    	$(function() {
    		$('img').hover(function() {
    			$(this).stop().animate({'width':'640px','height':'480px'},'slow');
    		},function() {
    			$(this).stop().animate({'width':'320px','height':'240px'},'slow');
    		});
    	});
    </script>
    </head>
    <body>
    <img src="http://www.autoclub.com.au/uploaded_images/citroen-c-metisse-concept-722498.jpg" alt="" />
    </body>
    </html>
    Now all you need is the jQuery js file.

  7. The Following User Says Thank You to Snookerman For This Useful Post:

    simonjones (12-13-2008)

  8. #5
    Join Date
    Jul 2007
    Posts
    35
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Thank You

    Thanks ever so much Snookerman and Medyman!! Very grateful for this.

    You're a very helpful lot here. Hope when I'm clever, I will be too.

    Thanks again.

  9. #6
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    2,023
    Thanks
    17
    Thanked 319 Times in 318 Posts
    Blog Entries
    3

    Default

    You're welcome! Remember to go to the first post in this thread, edit, go advanced and add the Resolved prefix to the title.

  10. The Following User Says Thank You to Snookerman For This Useful Post:

    simonjones (12-14-2008)

  11. #7
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    Simonjones...

    The code that Snookerman suggested is fine in principle. But, it's not very flexible for multiple implementations. If you're only trying to do the effect on one image, then it's fine because you can hard code the dimensions. But, if you're doing it on several images, his approach is a bit cumbersome.

    I took the liberty of coding a more dynamic script below. It still uses jQuery and Snookerman's code. But I've made all the dimensions into variables. At the top of the script, you would specify the scale by which you want to "zoom" in the growBy variable. Then, for any image that you wanted the effect to apply to, you would give it a class of "zoom". The script takes care of everything else:

    Example:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>Scaling Images with jQuery</title>
    <script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.2.6.pack.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
       $('img.zoom').hover(function() {
    		growBy = .35 // Percentage
    		origWidth = $(this).width();
    		origHeight = $(this).height();
    		zoomWidth = origWidth + (origWidth*growBy);
    		zoomHeight = origHeight + (origHeight*growBy);
    		console.log(zoomWidth + " + " + zoomHeight);
    		$(this).stop().animate({'width':''+zoomWidth+'','height':''+zoomHeight+''},'normal');
    		},function() {
    		$(this).stop().animate({'width':''+origWidth+'','height':''+origHeight+''},'normal');
    	});
    });
    </script>
    </head>
    <body>
    
    <img src="http://tinyurl.com/57xe5g" class="zoom" />
    
    </body>
    </html>

  12. The Following User Says Thank You to Medyman For This Useful Post:

    simonjones (12-14-2008)

  13. #8
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    2,023
    Thanks
    17
    Thanked 319 Times in 318 Posts
    Blog Entries
    3

    Default

    Medyman, there is a problem with your script. Since your hover function gets the width and height of the image every time it runs, it will keep adding to the values and after a few times hovering the image, it will get huge. You can see this by checking the console since you've forgot to remove the console.log bit. Here is the fix:
    Code:
    <script type="text/javascript">
    $(document).ready(function(){
    	growBy = .35; // Percentage
    	origWidth = $('img.zoom').width();
    	origHeight = $('img.zoom').height();
       $('img.zoom').hover(function() {
    		zoomWidth = origWidth + (origWidth*growBy);
    		zoomHeight = origHeight + (origHeight*growBy);
    		$(this).stop().animate({'width':''+zoomWidth+'','height':''+zoomHeight+''},'normal');
    		},function() {
    		$(this).stop().animate({'width':''+origWidth+'','height':''+origHeight+''},'normal');
    	});
    });
    </script>
    I also removed the console.log and added a semicolon that you forgot after the growBy variable. It should now work fine.

  14. The Following User Says Thank You to Snookerman For This Useful Post:

    simonjones (12-14-2008)

  15. #9
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    Ahh, good catch Snookerman. You're very right. If the user mouses-over and then back on really quickly, the width and height get recalibrated. But your proposed solution will again only work for a single instance.

    If I have time later, I'll post some revised code.

  16. The Following User Says Thank You to Medyman For This Useful Post:

    simonjones (12-14-2008)

  17. #10
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    Simon, the below code, in my estimation, should work nicely. It's not really well tested so there might still be some errors in my logic. I'm sure Snookerman will correct them if there are.

    Again, all you need to do after properly linking to these scripts is add class="zoom" to any image you want to apply this effect to.

    Code:
    <script type="text/javascript">
    $(document).ready(function(){
    	// Scale
    	zoom = .5
    	
    	// Variables
    	var h; var w;
    	
    	// Hover
    	$('img.zoom').mouseover(zoomIn);
    	$('img.zoom').mouseout(zoomOut);
    	
    	// Zoom In
    	function zoomIn() {
    		h = $(this).height();
    		w = $(this).width();
    		
    		$(this).stop().animate({'width':''+(w + (w*zoom))+'','height':''+(h + (h*zoom))+''},'normal');
    	}
    	
    	// Zoom Out
    	function zoomOut() {
    		$(this).unbind('mouseover')
    		$(this).stop().animate({'width':''+w+'','height':''+h+''},'normal', 'swing', function() {
    			$(this).mouseover(zoomIn);
    		});
    	}
    	
    });
    </script>

  18. The Following User Says Thank You to Medyman For This Useful Post:

    simonjones (12-14-2008)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •