Results 1 to 5 of 5

Thread: how to put effect onmouseover changing background?

  1. #1
    Join Date
    Jul 2009
    Location
    Binus University
    Posts
    472
    Thanks
    78
    Thanked 21 Times in 21 Posts

    Default how to put effect onmouseover changing background?

    I have this script to change background image onmouseover:

    Code:
    <html>
    <head>
    <style type="text/css">
    #test	{
    	background-image:url(images/background-2.jpg);
    	height:1300px;
    	width:1800px;
    }
    </style>
    <script type="text/javascript">
    function changeBgImage (image, id) {
    	var element = document.getElementById(id);
    	element.style.backgroundImage = "url("+image+")";
    }
    </script>
    </head>
    <body>
    <div 
        id="test" 
        onMouseOver="changeBgImage('images/background-2.jpg', 'test')"
        onMouseOut="changeBgImage('images/background-3.jpg', 'test')">
    </div>
    </div>
    </body>
    </html>
    How to add some effect like fade for example. I know we can use jquery, the problem is i'm not really understand how to make it work. Example or tutorial should be nice. Thanks a lot
    _____________________

    David Demetrius // davejob
    _____________________

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    this should start you off

    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" xml:lang="en" lang="en">
    <head>
    <title>Div Swop</title>
    
    <style type="text/css">
    #swop1, #swop2 { position:absolute; top:50px; left:50px; width:150px; height:150px; }
    #swop1 { background:url('images/1.jpg'); }
    #swop2 { background:url('images/2.jpg'); display:none; }
    </style>
    
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript">
    	$(document).ready(function()
    		{
    		$('#swop1').mouseover(function(){ $('#swop2').fadeIn(500)});
    		$('#swop2').mouseout(function(){ $('#swop2').fadeOut(500)});
              	});
    </script>
    
    </head>
    <body>
    
    <div id="swop1"></div>
    <div id="swop2"></div>
    
    </body>
    </html>
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  3. #3
    Join Date
    Jul 2009
    Location
    Binus University
    Posts
    472
    Thanks
    78
    Thanked 21 Times in 21 Posts

    Default

    hmm, thx beverleyh for your reply.
    I have already tried to use the script you gave to me, but nothing happen? is there something you forgot?

    Thank you very much...
    _____________________

    David Demetrius // davejob
    _____________________

  4. #4
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    I don't think so - it works on my local machine using my own images as referenced (only quickly tested in IE8, Firefox, Safari and Chrome and all those browsers are working).

    Have you definitely replaced the CSS background URL images with your own and checked the paths are correct?

    Maybe change the URLs to absolute and surround with "images/1.jpg" instead of 'images/1.jpg' too
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  5. #5
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    some servers also have problems with filenames that start with numbers - not common, but it happens

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
  •