Forum Moderators: open

Message Too Old, No Replies

Change Image Height with Javascript

innerHTML works but not practical

         

kevinkp7

2:54 pm on May 6, 2009 (gmt 0)

10+ Year Member



This is a really fun idea but I need some streamling/more effective javascripting (something other than innerHTML) to ever think of using in a site. It's called RooftopView where as you scroll the images are manipulated to give you a 3D effect.

Is there a better way to change img height?

Here's the code:

<body style="font-family:Verdana, Geneva, sans-serif;">
<div align="center"><h2>scroll down to see RooftopView</h2></div>
<table><tr><td height="700"></td></tr></table>

<div style="width: 500px; margin:0 auto 0 auto;">
<div id="dTop" style="margin:0 0 0 0; "></div>
<div id="dMiddle" style="margin:0 0 0 0; height:50px; background:url(http://example.com/projects/rooftopImages/middle.gif) no-repeat; width:500px;"><h3 style="margin:0 0 0 0; padding:12px 0 0 15px; color:#666;">RooftopView</h3></div>
<div id="dBottom" style="margin:0 0 0 0;"></div>
</div>

<table><tr><td height="900"></td></tr></table>

<script>

function rooftop () {

var initDivV=findPos(document.getElementById('dMiddle'));
var midpoint=f_clientHeight()/2;
var currentDivV=(initDivV-f_scrollTop());
var constant=midpoint/110;
var difference=currentDivV-midpoint;
var divH=Math.round(difference/constant);
if(divH<0){
document.getElementById('dTop').style.display='none';
document.getElementById('dBottom').style.display='';

//THIS PART************************************
document.getElementById('dBottom').innerHTML='<img src="http://example.com/projects/rooftopImages/bottom.gif" height="'+(-divH)+'" width="500" >';
//********************************************

}
else{
document.getElementById('dTop').style.display='';
document.getElementById('dBottom').style.display='none';

//AND THIS PART NEDDS BETTER METHOD *******************
document.getElementById('dTop').innerHTML='<img src="http://example.com/projects/rooftopImages/top.gif" height="'+divH+'" width="500" >';
//********************************************

}

setTimeout('rooftop()', 200);

}
rooftop();

//-- From <snipped>
function findPos(obj) {
var curleft = curtop = 0;
if (obj.offsetParent) {
curleft = obj.offsetLeft
curtop = obj.offsetTop
while (obj = obj.offsetParent) {
curleft += obj.offsetLeft
curtop += obj.offsetTop
}
}
return curtop;
}

//-- cross browser functions from -- <snipped>
function f_clientWidth() {
return f_filterResults (
window.innerWidth ? window.innerWidth : 0,
document.documentElement ? document.documentElement.clientWidth : 0,
document.body ? document.body.clientWidth : 0
);
}
function f_clientHeight() {
return f_filterResults (
window.innerHeight ? window.innerHeight : 0,
document.documentElement ? document.documentElement.clientHeight : 0,
document.body ? document.body.clientHeight : 0
);
}
function f_scrollLeft() {
return f_filterResults (
window.pageXOffset ? window.pageXOffset : 0,
document.documentElement ? document.documentElement.scrollLeft : 0,
document.body ? document.body.scrollLeft : 0
);
}

function f_scrollTop() {
return f_filterResults (
window.pageYOffset ? window.pageYOffset : 0,
document.documentElement ? document.documentElement.scrollTop : 0,
document.body ? document.body.scrollTop : 0
);
}

function f_filterResults(n_win, n_docel, n_body) {
var n_result = n_win ? n_win : 0;
if (n_docel && (!n_result ¦¦ (n_result > n_docel)))
n_result = n_docel;
return n_body && (!n_result ¦¦ (n_result > n_body)) ? n_body : n_result;
}


</script>

</body>

[edited by: whoisgregg at 3:17 pm (utc) on May 6, 2009]
[edit reason] Whoops, no URLs please. See TOS [webmasterworld.com] [/edit]

whoisgregg

3:37 pm on May 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Instead of recreating the image each time, you could just assign the image it's own ID and change the image height directly:

document.getElementById('dBottomImg').style.height = -divH+'px';

kevinkp7

3:45 pm on May 6, 2009 (gmt 0)

10+ Year Member



@whoisgregg - I've tried that and it won't work on the fly - that's why I'm confused. i've tried style.margin and style.padding too - nothing - at least not in Firefox

whoisgregg

3:53 pm on May 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In what way does it not work? Any javascript errors thrown or just nothing changes in the browser?

To debug this sort of thing, I would add a couple alerts to see if it's referencing the right object and to check the current value:

alert(document.getElementById('dBottomImg'));
alert(document.getElementById('dBottomImg').style.height);

kevinkp7

4:44 pm on May 6, 2009 (gmt 0)

10+ Year Member



alert(document.getElementById('dBottomImg')); shows something like '[object HTML element]'

alert(document.getElementById('dBottomImg').style.height); shows nothing

DrDoc

6:10 pm on May 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What does the following do?

alert(document.getElementById('dBottomImg').style.height);
document.getElementById('dBottomImg').style.height = "10px";
alert(document.getElementById('dBottomImg').style.height);

kevinkp7

6:16 pm on May 6, 2009 (gmt 0)

10+ Year Member



@DrDoc - the first alert is blank and the second is 10px

kevinkp7

6:33 pm on May 6, 2009 (gmt 0)

10+ Year Member



I got it -

I went ahead and put the img in <div id="dBottom"> and named it "bottom" like so:

<div id="dBottom">
<img name="bottom" src="img/bottom2.gif" height="110" width="500" >
</div>

and then changed

document.getElementById('dBottom').innerHTML='<img...

to

document.images['bottom'].height=-divH;

Thanks guys

whoisgregg

7:33 pm on May 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad you got it sorted. :)