简体   繁体   中英

how to set scroll on the div by setting height to percent

I have a div i want to make it scroll top . when i set its height to pixel that time i am getting the scroll but when i am setting it to percent i am not getting scroll

<div id="lblAlert" runat="server" class="warning-message" style="overflow:auto" >

// working code

scrollfun()
{
lblAlert.style.height = 65; 
}
// not working code
scrollfun()
{
lblAlert.style.height = '2%'; 
}

How will i do this,how can i convert pixel to percent ?? please help

try to get height of parent element using

var element_Height = $(element).parent().css('height');

and calculate scroll height from it using

height = (2 / 100) * element_Height;

This will give you height to scroll.

Heres my jsFiddle that does what you want http://jsfiddle.net/F8Qpx/

changeHeight = function (){
var obj = document.getElementById('lblAlert');
var parent = obj.parentNode;
obj.style.height = (parent.offsetHeight/100)*2 + "px";//Make 2%
}​

This code should work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM