简体   繁体   English

如何通过将height设置为百分比来设置div上的滚动

[英]how to set scroll on the div by setting height to percent

I have a div i want to make it scroll top . 我有一个div我想让它滚动顶部。 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/ 继承人我的jsFiddle做你想做的事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 这段代码应该有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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