image position to stay on top of page (Full Version)

All Forums >> [Web Development] >> Cascading Style Sheets



Message


malky800 -> image position to stay on top of page (3/10/2008 22:47:58)

Is there a way to keep an image on top of the page in css even as you scroll down the page?




jaybee -> RE: image position to stay on top of page (3/11/2008 9:38:11)

body {
background-image: url(xxxxxxxx.gif);
background-attachment: fixed
}




malky800 -> RE: image position to stay on top of page (3/11/2008 11:22:16)

I want that even if I'm halfway down the page the image should move and still stay on top.
I tried background-attachment:scroll
I have this css style in a td. Is that why it's not working? Do you need it to be in only certain element tags?




rdouglass -> RE: image position to stay on top of page (3/11/2008 14:47:43)

I use a JS on a demo page I have running if you want to look at it; it may suit what you need. I use it to hold a moving mouse-over help window but I'm sure you could put an image or two or three in there just fine.

http://www.rogerdouglass.com/genericContactLite/genericContactLiteConfig.asp

Log in (UN and PW on page) and click the "Configuration" menu and then scroll up and down. You should see the window following the screen. It's a fairly common JS effect but IMO should not be overdone.

That page could be a little hard to follww in the code but I got the original (and hacked at it) from dynamicdrive.com .

The essence is the JS here:

<script type="text/javascript">

/***********************************************
* Floating Top Bar script- © Dynamic Drive (www.dynamicdrive.com)
* Sliding routine by Roy Whittle (http://www.javascript-fx.com/)
* This notice must stay intact for legal use.
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

var persistclose=0 //set to 0 or 1. 1 means once the bar is manually closed, it will remain closed for browser session
var startX = 30 //set x offset of bar in pixels
var startY = 5 //set y offset of bar in pixels
var verticalpos="fromtop" //enter "fromtop" or "frombottom"

function iecompattest(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function get_cookie(Name) {
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) {
offset += search.length
end = document.cookie.indexOf(";", offset);
if (end == -1) end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}

function closebar(){
if (persistclose)
document.cookie="remainclosed=1"
document.getElementById("topbar").style.visibility="hidden"
}

function staticbar(){
	barheight=document.getElementById("topbar").offsetHeight
	var ns = (navigator.appName.indexOf("Netscape") != -1) || window.opera;
	var d = document;
	function ml(id){
		var el=d.getElementById(id);
		if (!persistclose || persistclose && get_cookie("remainclosed")=="")
		el.style.visibility="visible"
		if(d.layers)el.style=el;
		el.sP=function(x,y){this.style.left=x+"px";this.style.top=y+"px";};
		el.x = startX;
		if (verticalpos=="fromtop")
		el.y = startY;
		else{
		el.y = ns ? pageYOffset + innerHeight : iecompattest().scrollTop + iecompattest().clientHeight;
		el.y -= startY;
		}
		return el;
	}
	window.stayTopLeft=function(){
		if (verticalpos=="fromtop"){
		var pY = ns ? pageYOffset : iecompattest().scrollTop;
		ftlObj.y += (pY + startY - ftlObj.y)/8;
		}
		else{
		var pY = ns ? pageYOffset + innerHeight - barheight: iecompattest().scrollTop + iecompattest().clientHeight - barheight;
		ftlObj.y += (pY - startY - ftlObj.y)/8;
		}
		ftlObj.sP(ftlObj.x, ftlObj.y);
		setTimeout("stayTopLeft()", 10);
	}
	ftlObj = ml("topbar");
	stayTopLeft();
}

if (window.addEventListener)
window.addEventListener("load", staticbar, false)
else if (window.attachEvent)
window.attachEvent("onload", staticbar)
else if (document.getElementById)
window.onload=staticbar
</script>


That JS and the stuff inside the DIV <div id="topbar">. Just there is other JS on that page as well that swaps out the help text and that isn't necessary for the following window.

Hope it helps.




jaybee -> RE: image position to stay on top of page (3/11/2008 15:16:29)


quote:

ORIGINAL: malky800

I want that even if I'm halfway down the page the image should move and still stay on top.
I tried background-attachment:scroll
I have this css style in a td. Is that why it's not working? Do you need it to be in only certain element tags?


Yep, that's exactly why it's not working. A td is a table cell, when you scroll the page the table and the cell goes with it.




malky800 -> RE: image position to stay on top of page (3/11/2008 22:01:32)

basically the only thing I can scroll is a body background image?
on my site there is background images to all the tables so it is overlapping any body image I put in.
Is there a way to define which image should be the top layer?

Is there any other tag I can put it in that would allow the image to scroll?




jaybee -> RE: image position to stay on top of page (3/11/2008 22:17:22)

Not that I can think of. I reckon Roger's solution above is the way to go.

There are ways to put a box above everything else with css using z-index but I can't think how you'd stop it scrolling with the page.




Page: [1]

Valid CSS!




Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI
0.046875