JavaScript - Javascript Background-image Rollovers
Hi guys,
Just wanted to know (seeming im a bit of a jscript beginner). it is possible to have a background image rollover, and if so how. Thanks. Similar TutorialsSay we have an image called imagefile.jpg, which we want to display when the user mouses over an already-displayed image.Now I want to preload more than just one image; for example, in a menu bar containing multiple image rollovers,or if I try to create a smooth animation effect what should i do? I tried the following code but it doesn't work. Please help 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=iso-8859-1" /> <title>Untitled Document</title> <script language="JavaScript"> function preloader() { // counter var i = 0; // create object imageObj = new Image(); // set image list images = new Array(); images[0]="1.jpg" images[1]="2.jpg" images[2]="3.jpg" images[3]="4.jpg" // start preloading for(i=0; i<=3; i++) { imageObj.src=images[i]; } } </script> </head> <body onLoad="javascript:preloader()"> <a href="#" onMouseOver="javascript:document.img01.src='1.jpg'"> <img name="img01" src="4.jpg"></a> </body> </html> Mainly I want to create the animation effect with the 4 images. Is that possible? Hi there, New to these forums, and to coding websites so be kind . My problem is, when using lightbox2 I press the back button and the rollover remains in the mouse-over position. I can "mouse over' it and then the rollover will disappear, it just seems to get stuck on. I really have no idea what is causing this, and any help would be greatly appreciated. Thanks alot, Joel HI, I am not very experienced with javascript and got stuck doing the following. I have placed some image rollovers on one of my pages..where when a user rolls over some text links..and image appropriate to that text is showen...when the mouse moves away from the links then a blank image is displayed as a place holder. this is all simple...here is where i am stuck. The place holder has a specific size...but the rest of the images that are displayed when the text is rolled over have different sizes, some are vertical some are hortizontal. i do not want to have to resize every image so that it properly fits the place holder...add the white borders and so on...so that the image is dispalyed properly. Is there a way to have the size be changed for each image...so that the size doesnt get inherited from the place holder, but so that the script applies the size associated with each individual image. example place holder is 20 x 20 image 1 is 100 x 200 image 2 is 200 x 100 the place holder is loaded...when the mouse moves over the first link...image 1 replaces the place holder...but instead of taking on the size of 20x20 it has to be displayed at its own size of 100 x 200... how can i do this? thank you for all of your help First off, I am new here - so, an introduction. My name is David Lubofsky. I am a front-end developer in Chicago, Illinois. I have done numerous JS rollovers before, as well as image rollovers triggered by text links. However, I am having a problem combining the two. The best example I have found of what I am trying to achieve is here - http://theworldcovered.ivyfunds.com (top right area). I have no issue achieving part of this - where the link mouseovers trigger the image swap. My issue is making both the link and the image cause the rollover. The method I use for image swap on text link hover is seen here - http://www.htmlite.com/faq011.php This is what I want, except I want the image to also swap if the image is rolled over, as in the first example. Hope this was clear - I could really use some guidance on this. Thanks all! David Hi, everyone. I've just finally adjusted the way that the background images is laid out. my problem is that the image seems to load in original size and then align to the screen's width. I'm using a javascript code that I got from http://stackoverflow.com/questions/1...g-aspect-ratio and it is working the way I want it to, mostly. My problem is that I'm not sure how to keep the image from exceeding 100% height of the windows without squashing the aspect ratio? I also need to figure out how to fix the visible action of the image being loaded in it's original size? If we can't get the image to load WHILE at the size I want it to be, can we at least 'hide' the image until the loading have been done? I tried to tweak the codes to include the height limitation but it did not seem to have made any differences. This is my first time actually using javascript so please be easy on me, heh My site is at http://www.lost-ear-studio.com (if you refresh the site, you will see random background images being swapped out using php). Code: <script> function resizeToMax(id){ myImage = new Image() var img = document.getElementById(id); myImage.src = img.src; if(myImage.width > myImage.height){ img.style.width = "100%"; } else { img.style.height = "100%"; } } </script> Code: <img id="bg" src="randombg.php" onload="resizeToMax(this.id)"> Code: #bg{ position:fixed; left:0px; top:0px; z-index:-1; } Hello. The code below lets a user select a drop down option and a picture changes depending on the selection. I am trying to make it so when you select an option on the dropdown the picture shows up in the center of the screen and in the background behind the text. Here is the code. Code: <html> <head> <style type='text/css'> #txt {background-repeat:no-repeat;width:200px;height:200px;} </style> <script type='text/javascript'> function div_bg(team){ document.getElementById('txt').style.backgroundImage="url(images/" + team + ".jpg)"; } </script> </head> <body> <select name='team' onChange='div_bg(this.value);'> <option name='arsenal' value='arsenal'>Arsenal</option> <option name='liverpool' value='liverpool'>Liverpool</option> </select> <div id='txt'></div> </body> Thanks in advance!! I have the following 3 buttons on my page. I need when on hover on each respective button, the button to light up with a bg image and a slide out rollover in the top direction. How can I achieve this? <div style="padding-left:45px;width:852px;"> <a href=".html" class="Button"> <div style="text-align:center;padding-top:15px;float:left;width:270px;height:45px;background-image:url(images/homeBG.png);background-repeat:no-repeat"> <span id="homeButtonText">Question 1</span><br /> </div> </a> <a href="" class="Button"> <div style="margin-left:18px;text-align:center;padding-top:15px;float:left;width:270px;height:45px;background-image:url(images/homeBG.png);background-repeat:no-repeat"> <span id="homeButtonText">Question 2</span><br /> </div> </a> <a href="" class="Button"> <div style="text-align:center;padding-top:15px;float:right;width:270px;height:45px;background-image:url(images/homeBG.png);margin-right:5px;background-repeat:no-repeat"> <span id="homeButtonText">Question 3</span><br /> </div> </a> </div> Good day Lets say I have a page with 3 thumbnails and one big div. When click in one of the thumbnails the image of it must appear inside the big div. Of course there are 3 images (little ones for the thumbnails) and 3 big images for the real size ones. Script this is simple for me but imposible if I have more than 100 pages within 6 images inside them (3 little and 3 real size for 3 thumnail divs and one single big div). There must be some kind of order to just script the divs of the thumbnail divs and the real size image div in all the pages at ones, otherwise I have to creat ID's to every single div inside every single page! (4 id's to one page and I have more than 100 pages!, this is inpractical because is posible that in the future the pics change, is more than 400 id's) For example: for FIRST.html big div (div id="img001bg") thumb1 div (div id="th_img001" thumb2 div (div id="th_img002" thumb3 div (div id="th_img003" SECOND.html THIRD.html ... HUNDRED.html ... ! There must be some solution using somehow the functions, please help! Thank you to all Hi everyone, this is my first post on this forum and is also my first attempt to use javascript. When it comes to javascript I am a complete noob, but before you push the back button I just thought I'd let you know I just spent a few hours in some javascript tuts so I could *at least* have some idea of what you and I are talking about. Ok, here's my question: How can I make a disjointed div become and remain visible when I hover over it's parent div? The Only time I want that div to go back to being invisible is when I hover over another parent div causing it's child div to become visible. Here's the example (only tested in FF 3.5.2) : http://www.e-uphoria.com/demo_two.html I used :hover in css to show the disjointed div (the red box) that I want to become visible via javascript. I managed to achieve this effect with css, but when the user moves the mouse out of the browser window the div disappears because the hover is no longer activated. This is why I need javascript, so the user can do anything he or she wants outside of the browser window and the "hover" will still be activated revealing the content of the div. Here's the example of the css only version (again, only tested in FF 3.5.2) : http://www.e-uphoria.com/demo.html I messed around with some onmouseover and onmouseoff within the html and realized I could achieve the desired effect with some really messy and inefficient code. I would prefer to have the javascript in an external file and not looking like it was written by an idiot. If anybody could help me out with this that would be awesomely awesome. Thanks! Using Dreamweaver CS3 (WinXP), is there a simple way to add captions to disjointed rollovers? (The disjointed rollovers are created using image swap behavior) My aim is to click on a thumbnail, and when the image is projected on the poster, I want a caption appearing underneath. Can you please help me with this, here is my complete code Thanks [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>Untitled Document</title> <script type="text/javascript"> <!-- function MM_preloadImages() { //v3.0 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array(); var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++) if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}} } function MM_swapImgRestore() { //v3.0 var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc; } function MM_findObj(n, d) { //v4.01 var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) { d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);} if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n]; for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); if(!x && d.getElementById) x=d.getElementById(n); return x; } function MM_swapImage() { //v3.0 var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3) if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];} } //--> </script> </head> <body onload="MM_preloadImages('images/fullscreen/1z.jpg','images/fullscreen/2z.jpg')"> <p><img src="images/fullscreen/1z.jpg" alt="HL" name="pic1" width="114" height="102" id="pic1" onmouseover="MM_swapImage('poster','','images/fullscreen/1z.jpg',1)" onmouseout="MM_swapImgRestore()" /> <img src="images/fullscreen/2z.jpg" alt="HL" name="pic2" width="114" height="102" id="pic2" onmouseover="MM_swapImage('poster','','images/fullscreen/2z.jpg',1)" onmouseout="MM_swapImgRestore()" /></p> <p> </p> <p><img src="images/fullscreen/3z.jpg" alt="HL" name="poster" width="500" height="375" id="poster" /></p> </body> </html> [code] i am creating a header for a webpage. the header has two text menus (no images). the first menu (menu A) is like this: link 1a / link 2a / link 3a the second menu (menu B) is like this: link 1b link 2b link 3b i would like to use javascript/css to do the following: when you rollover link 1a, three things happen: link 1a changes color, link 1b changes color, and and link 1b adds a line of text (so it reads: link 1b : text 1b) i would also like- and this is where i am having trouble- for the following to happen: when you rollover link 1b, link 1b changes color and adds the additional text, as well as link 1a changes color. it doesn't seem so bad when i write it out here, but i can't figure out how to do it. any help is appreciated. Hi all, I was wondering if anyone knew were I can get a javascript that will adjust the background image dependant on the users screen resolution like whats used on this site http://gregorywood.co.uk/journal/chilli-babies Thanks in advance!! Kyle I have 3 background images, and 1 is picked at random to be the background image of the body when loading the page.My code is below. Right now it's just white, can't get any pictures to load. I have checked the URLs and they are correct. Can someone help out? Here is my code in the header: Code: <script type="text/javascript"> function getBackground(){ var bgimg = new Array(); bgimg[0] = "background1"; bgimg[1] = "background2"; bgimg[2] = "background3"; var random = Math.floor(Math.random() * bgimg.length); var imgurl = bgimg[random]; document.body.style.backgroundImage = 'url(' + imgurl + ')'; } </script> body: Code: <body onload="getBackground()"> I have a 16x16 solid color yellow image - .gif type. I'm using it as a background for a div. I'd like to make it transparent but can't figure out how to do. I have Paint.net and follow its instructions to make the image 50% transparent but afterwards, it's completely clear with no hint of yellow. How can I make a transparent solid image? I'd like it about 50% transparent. Thanks... PHP Code: .xstooltip { visibility: hidden; position: absolute; top: 0; left: 0; z-index: 2; font: normal 8pt sans-serif; padding: 3px; border: solid 1px; /*background-color: #DBDBDB;*/ /*background-color: #FFFF77;*/ background-repeat:repeat; background-image:url(/yellow_help.gif); } After you click on the last button 3 times it should change the background to the zombies image however it isn't working. I haven't found tutorials for doing it exactly as I am attempting it but I don't see why this isn't working. It runs through the code just fine and the button works as intended except for the fact that the background image isn't changing. I am still pretty new to JavaScript however so I feel I may just being calling the image incorrectly. Code: <html> <title>Welcome :D</title> <head> <style type="text/css"> body { background-color:E6E6E6; } </style> <script type="text/javascript"> //Pre loading images to be used with check boxes. function preLoad(){ Eyeball= new Image(400,200) Eyeball.src = "Eyeball.jpg" Smileys = new Image(400,200) Smileys.src = "Smileys.gif" goodAfternoon = new Image(400,200) goodAfternoon.src = "Good Afternoon.gif" goodAfternoonFinal = new Image(400,200) goodAfternoonFinal.src = "Good Afternoon.gif" zombies=new Image() zombies.src = "Zombies.jpg" } function validate(){ if(document.getElementById("cb1").checked){ document.goodAfternoon.src=Eyeball.src; }else{ document.goodAfternoon.src=Smileys.src; } } function reset(){ cb1.checked=false; document.goodAfternoon.src=goodAfternoonFinal.src; } var i=0; function myalert(){ i++ if(i == 1){ alert("I dare you to press me again...."); } if(i == 2){ } if(i == 3){ i = 0; document.body.background = "zombies.src"; } } </script> </head> <body onLoad="javascript:preLoad()"> This is where I learn a lot of my coding for Java, JavaScript, and HTML <a href="http://www.thenewboston.com/"><img src="theNewBoston.gif"></a> </br> "Check" me out;) <input type="checkbox" id="cb1" onClick="validate()" /> <img src="Good Afternoon.gif" name="goodAfternoon"> <input type="button" id="cb2" value="RESET" onClick="reset()"/> </BR></br></br></br></br> Hello, thank you for visiting my webpage I hope you like it. <input type="button" onClick="myalert()"value="CLICK me"/> </body> </html> Hi Guys, I would like to be able to change my webpage background image according to the screen resolution the user uses so: if screen resolution is greater than or equals to 1200*600 then background = mybackground.jpg no-repeat else background = #000000 THANKS A MILLION! I found a great little starfield script made with javascript and canvas3d. however i can't for the life of me figure out how to implement a background image inside the canvas. is there some command that tells it to use some feature of canvas, that if otherwise not called, won't allow normal graphics? his whole starfield is generated with a search engine form , and appears to just use a text period to represent stars. here's a link to his script: http://seb.ly/demos/canvas3d/canvas3d2.html how do i get a background image in that? or is it impossible? i tried putting it in the body tag, but it just flashed the background image on the screen for a half a second and then went back to showing only the black background. i tried removing the background body color and replacing it with only background-image ccs, same problem. i tried div positioning it, but the div just covered up the starfield. Im going nuts with this, first im starting of by just putting a background image in the tb using JavaScript, but it won't work! Heres what i got in my 'name.js' file i got var name=new Array() name='name.jpg' and in my HTML i got <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> </head> <body> <div> <table> <script type="text/javascript" language="JavaScript" src="name.js"> document.write('<td id="name2" background="' + name + '">'); </script> </td> </table> </div> </body> </html> Where have a gone wrong?? If I type Hello World into the name.js file and put it in a tb it works fine, im confused thanks, Paul Hey, everyone! I found this script that works great for what it's for. However, I would like to use it for background images. Can someone tell me how to adjust this script so it can be used for backgrounds? Thanks in advance! Web Page Code Code: <html> <head> <title>slayeroffice | code | image cross fade redux</title> <link rel="stylesheet" type="text/css" href="xfade2_o.css"> <script type="text/javascript" src="xfade2.js"></script> </head> </div> <body background="id=imageContainer"> <div id="imageContainer"> <img src="http://slayeroffice.com/code/imageCrossFade/img1.jpg"> <img src="http://slayeroffice.com/code/imageCrossFade/img2.jpg"> <img src="http://slayeroffice.com/code/imageCrossFade/img3.jpg"> <img src="http://slayeroffice.com/code/imageCrossFade/img4.jpg"> <img src="http://slayeroffice.com/code/imageCrossFade/img5.jpg"> <img src="http://slayeroffice.com/code/imageCrossFade/img6.jpg"> </div> </body> </html> Style Sheet 1 Code: #imageContainer { height:309px; } #imageContainer img { display:none; position:absolute; top:0; left:0; } Style Sheet 2 Code: #imageContainer { position:relative; margin:auto; width:500px; border:1px solid #000; } JavaScript Code: window.addEventListener?window.addEventListener("load",so_init,false):window.attachEvent("onload",so_init); var d=document, imgs = new Array(), zInterval = null, current=0, pause=false; function so_init() { if(!d.getElementById || !d.createElement)return; css = d.createElement("link"); css.setAttribute("href","xfade2.css"); css.setAttribute("rel","stylesheet"); css.setAttribute("type","text/css"); d.getElementsByTagName("head")[0].appendChild(css); imgs = d.getElementById("imageContainer").getElementsByTagName("img"); for(i=1;i<imgs.length;i++) imgs[i].xOpacity = 0; imgs[0].style.display = "block"; imgs[0].xOpacity = .99; setTimeout(so_xfade,1000); } function so_xfade() { cOpacity = imgs[current].xOpacity; nIndex = imgs[current+1]?current+1:0; nOpacity = imgs[nIndex].xOpacity; cOpacity-=.05; nOpacity+=.05; imgs[nIndex].style.display = "block"; imgs[current].xOpacity = cOpacity; imgs[nIndex].xOpacity = nOpacity; setOpacity(imgs[current]); setOpacity(imgs[nIndex]); if(cOpacity<=0) { imgs[current].style.display = "none"; current = nIndex; setTimeout(so_xfade,1000); } else { setTimeout(so_xfade,50); } function setOpacity(obj) { if(obj.xOpacity>.99) { obj.xOpacity = .99; return; } obj.style.opacity = obj.xOpacity; obj.style.MozOpacity = obj.xOpacity; obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")"; } } Hello, I've just finished my website. But my random background images load properly sometimes, and sometimes they load as a half. Here is my website: http://www.finnhaverkamp.com/ Here is the relevant HTML: Code: <!--open random background script--> <script type="text/javascript"> var randnum = Math.random(); var inum = 7; var rand1 = Math.round(randnum * (inum-1)) + 1; var images = new Array; images[1] = "background1.jpg"; images[2] = "background2.jpg"; images[3] = "background3.jpg"; images[4] = "background4.jpg"; images[5] = "background5.jpg"; images[6] = "background6.jpg"; images[7] = "background7.jpg"; var image = images[rand1]; function chBackgr() { document.body.style.backgroundImage = 'url(' + image + ')'; } onload = chBackgr; <!--close random background script--> It's weird. Because my background image is certainly random. It's just that sometimes only half of the image loads. Really strange. Any help is appreciated. Thank You. |