简体   繁体   English

如何设置元素的宽度等于窗口宽度的一半

[英]How to set element's width equal to half of the window's width

I am trying to automatically generate rounded values with the floor function but it stays static. 我正在尝试使用floor函数自动生成舍入值,但它保持静态。 Can you please help me how to improve my code ? 您能帮我改善代码吗? Thank you very much. 非常感谢你。

jQuery: jQuery的:

$(document).ready(function() {

    width_split = $(document).width();
    width_split = Math.floor(width_split / 2);

    $(document).resize(function() {
        width_split = $(document).width();
        width_split = Math.floor(width_split / 2);  
    });

});

HTML: HTML:

<p style="position:absolute; right:"+width_split+"px;">
   .content
</p>

You have to set right value inside your JS not in the HTML DOM 您必须在JS而不是HTML DOM中设置right

Try: 尝试:

HTML: HTML:

<p id="mypar" style="position:absolute;">
   .content
</p>

JS: JS:

$(document).ready(function(){

  width_split = $(document).width();
  width_split = Math.floor(width_split / 2);
$(document).resize(function(){
  width_split = $(document).width();
  width_split = Math.floor(width_split / 2);  
  $('#mypar').css("right", width_split+'px');//ADD THIS
});

$('#mypar').css("right", width_split+'px');//ADD THIS
});

DEMO DEMO

Apply the width_split for <p> tag by jquery, the way you are appying width_split at inline p tag is wrong. 通过jquery为<p>标签应用width_splitwidth_splitp标签上应用width_split的方式是错误的。 Something like should be 应该是这样的

Html HTML

   <p id="para" style="position:absolute"; >
       .content
    </p>

js JS

$("#para").css("right", width_split + "px");

instead of 代替

<p style="position:absolute; right:"+width_split+"px;">
///--------------------------this is wrong---^

You should also update HTML tag 您还应该更新HTML标记

$('pID').css('right', width_split.toString()+'px');

Inside the document.resize function. 在document.resize函数内部。

The simpler solution 更简单的解决方案

Select each element you wish to adjust and apply the style directly 选择您要调整的每个元素并直接应用样式

$('.your-selector').css({theProperty: halfWidth});

But this way you have to modify the js for every new element that you add to the html. 但是通过这种方式,您必须为添加到html中的每个新元素修改j​​s。

An generic approach would be 通用方法是

Put a marker (MRK) class on "reactive" objects, and include in the marker also the css property you want to set to be width/2 (eg. MRK-left, or MRK-margin-left) 在“反应性”对象上放置标记(MRK)类,并在标记中还包含要设置为width / 2的css属性(例如MRK-left或MRK-margin-left)

Then on window resize, search for the elements that contain the marker, extract the css property, and set it on them. 然后在调整窗口大小时,搜索包含标记的元素,提取css属性,然后在其上进行设置。

DEMO fiddle 演示小提琴

$(window).on('resize', function () {
    var halfWidth = $(window).width() / 2;
    $('*[class*=MRK-]').each(function(){  // selects all elements having a class  
       var cls = $(this).attr('class')   // property that contains MRK
       var cssPropName = /MRK-([\w-]+)/.exec(cls)[1]; // extracts the info MRK
       var css = {};
       css[cssPropName] = halfWidth;  // creates the styleobject with the info
       $(this).css(css);
    });
});

In html you would write: 在html中,您可以这样写:

<div class="MRK-left"></div>

or 要么

<p class="MRK-margin-left"></p>

The result at 800px width would be: 宽度为800px的结果为:

<div class="MRK-left" style="left: 400px"></div>

or 要么

<p class="MRK-margin-left" style="margin-left: 400px"></div>

And you can change like this any property without changing the js logic 您可以像这样更改任何属性,而无需更改js逻辑

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

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