简体   繁体   English

jQuery - 根据屏幕宽度添加像素到高度

[英]jQuery - add pixels to height according to screen width

I need to do this with jQuery. 我需要用jQuery做到这一点。

Let's say that I have simple div element in HTML like that: 假设我在HTML中有简单的div元素:

<div class="grow"></div>

CSS: CSS:

body, html {
width: 100%; 
height:100%;
min-height:100%
}

.grow {
height:20px; 
width:100%;
}

I need to write script where DIV grow will be growing according to the screen resolution. 我需要根据屏幕分辨率编写DIV grow脚本。 Starting from 90px's. 从90px开始。

Sth like this: 是这样的:

For 90px screen wide DIV height will be - 20px For 130px screen wide DIV height will be - 30px 对于90px屏幕宽DIV高度将为 - 20px对于130px屏幕宽度DIV高度将为 - 30px

so for any 4px's width height will grow 1px. 因此对于任何4px的宽度高度将增长1px。

I tried lots of solutions but without any luck. 我尝试了很多解决方案,但没有任何运气。 Any help will be appreciated. 任何帮助将不胜感激。

Try this 尝试这个

$(window).resize(function() {
    var bodyheight = $(document).height();
    var divHeight = (bodyheight-10)/2;
    $('.grow').css("height", divHeight+"px");;
});

here you go [http://jsfiddle.net/6KfHy/1/] 你去[http://jsfiddle.net/6KfHy/1/]

var baseWidth = 90;
var stepWidth = 4;
var baseHeight = 20;
var growHeightPerStep = 1;

function changeHeight() {
    var windowW = $(window).width();
    var diffWidth = windowW - baseWidth;
    var diffHeight = parseInt(diffWidth / 4, 10);

    $('.grow').css('height', baseHeight + diffHeight + 'px');
}

changeHeight();

$(window).resize(function() {
    changeHeight();
});

Try this 尝试这个

var width = (+$(window).width());
var divHeight = (width-10)/2;
$(".grow").height(divHeight);
var $grow = $('.grow'),
    $window = $(window);
$(window)
    .resize(function () {
        var windowWidth = $window.width();
        if (windowWidth > 90) {
            $grow.height(~~ (windowWidth / 4));
        }
    })
    .resize();

Trigger it the first time on dom ready. 在dom准备好的第一次触发它。

http://jsfiddle.net/techunter/Xug5A/ http://jsfiddle.net/techunter/Xug5A/

remark careful with .grow margins and paddings :) 注意.grow边距和填充:)

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

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