简体   繁体   English

如何摆脱黄色部分的空白?

[英]How to get rid of whitespace in the yellow portion?

 function Chat(id) { function resize() { var chat_emb = $("#" + id); var height = $(window).height() - 19; var width = $(window).width() - 19; chat_emb.height(height); chat_emb.width(width); var boxw = chat_emb.width() / 1.20; var boxh = chat_emb.height(); var boxtop = (boxh / 5) / 90; boxleft = (boxw / 5) / 90; var box = $("#cmsgs") var input = $("#input") var ulw = width - boxw var ulh = chat_emb.height() + 3; var ul = $("#ul"); var rightul = (ulw + boxw - width) + 10 //check if box has a height //if it does then no need to make element if (box.height()) { box.css("position", "absolute"); box.css("top", boxtop); box.css("left", boxleft) box.height(boxh) box.width(boxw); } else { var box = document.createElement("div"); box.id = "cmsgs"; document.getElementById(id).appendChild(box); var box = $("#cmsgs") box.css("position", "absolute"); box.css("top", boxtop); box.css("left", boxleft); box.height(boxh) box.width(boxw); } if (!input.height()) { var input = document.createElement("input"); input.setAttribute("id", "input"); document.getElementById(id).appendChild(input); input = $("#input") input.width(boxw - 5) input.css("position", "absolute"); input.css("left", boxleft); input.css("bottom", boxtop); } else { input = $("#input") input.width(boxw - 5) input.css("position", "absolute"); input.css("left", boxleft); input.css("bottom", boxtop); } if (ul.height()) { ul.height(ulh + 3); ul.width(ulw); ul.css("postition", "absolute"); ul.css("right", rightul); } else { var ul = document.createElement("div"); document.getElementById(id).appendChild(ul); ul.id = "ul"; ul = $("#ul"); ul.height(ulh + 6); ul.width(ulw); ul.css("position", "absolute"); ul.css("right", rightul); } } resize(); //resize event $(window).bind("resize", function() { resize() }); $(document).bind("resize", function() { resize() }); var box = $("#cmsgs"); box.css("background-color", "gray"); var input = $("#input") input.css("background-color", "#0FF"); var ul = $("#ul"); ul.css("background-color", "#FF0"); ul.css("padding", 0, 0, 0, 0, 0); ul.css("margin", 0, 0, 0, 0, 0); box.css("padding", 0, 0, 0, 0, 0); box.css("margin", 0, 0, 0, 0, 0); } Chat("chatbox") 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="chatbox"></div> 

As you can see when you run the snippet the yellow part has a lot of whitespace. 如您所见,运行代码段时,黄色部分有很多空白。 How can i get rid of it? 我如何摆脱它? I know this might be hard to understand because the code is kinda crammed in there. 我知道这可能很难理解,因为其中的代码有点塞满了。 So, if you have any trouble understanding ill explain more. 因此,如果您对理解疾病有任何疑问,请多解释。 I've tried to set margins and paddings to 0 but it doesn't seem to work. 我尝试将页边距和填充设置为0,但似乎不起作用。

 #chatbox{ position:absolute; height:100%; width:100%; top:0px; left:0px; } #cmsgs{ position:absolute; top:0px; left:0px; background-color:grey; width:80%; height:95%; } #input{ display:inline-block; position:absolute; bottom:0px; left:0px; height:5%; width:80%; background-color:rgb(0,255,255); } #ul{ position:absolute; right:0px; top:0px; height:100%; width:20%; background-color:yellow; } 
  <div id="chatbox"> <div id="cmsgs"></div> <input id="input"> <div id="ul"></div> </div> 

 #ul{
        position:absolute;
        height:100%;
        top:0px;
        right:0px;
        width:calc(100%-(width of grey block)px); //assuming you want a specific pixel width
        background-color:yellow;
    }

calculate or set the width of the grey block so that it is known (i would set it with a css percentage). 计算或设置灰色块的宽度,以便知道它(我将以css百分比设置它)。

use that to calculate the width of the yellow block. 用它来计算黄色块的宽度。 this can be done in several ways using javascript or css. 可以使用javascript或css以多种方式完成此操作。

Rather than absolute positioning, you can use position:relative on the parent div, and use a simple float: right. 除了绝对定位,您可以在父div上使用position:relative ,并使用简单的float:right。

This way, you wouldn't even need to know the width of anything in advance. 这样,您甚至不需要事先知道任何东西的宽度。

You could try below code. 您可以尝试以下代码。 Few changes in your code 您的代码很少更改

 function Chat(id) { function resize() { var chat_emb = $("#" + id); var height = $(window).height() - 19; var width = $(window).width() - 19; chat_emb.height(height); chat_emb.width(width); var boxw = chat_emb.width() / 1.20; var boxh = chat_emb.height(); var boxtop = (boxh / 5) / 90; boxleft = (boxw / 5) / 90; var box = $("#cmsgs") var input = $("#input") var ulw = $(window).width() - boxw var ulh = chat_emb.height() + 3; var ul = $("#ul"); var rightul = (ulw + boxw - $(window).width()) //check if box has a height //if it does then no need to make element if (box.height()) { box.css("position", "absolute"); box.css("top", boxtop); box.css("left", boxleft) box.height(boxh) box.width(boxw); } else { var box = document.createElement("div"); box.id = "cmsgs"; document.getElementById(id).appendChild(box); var box = $("#cmsgs") box.css("position", "absolute"); box.css("top", boxtop); box.css("left", boxleft); box.height(boxh) box.width(boxw); } if (!input.height()) { var input = document.createElement("input"); input.setAttribute("id", "input"); document.getElementById(id).appendChild(input); input = $("#input") input.width(boxw - 5) input.css("position", "absolute"); input.css("left", boxleft); input.css("bottom", boxtop); } else { input = $("#input") input.width(boxw - 5) input.css("position", "absolute"); input.css("left", boxleft); input.css("bottom", boxtop); } if (ul.height()) { ul.height($(window).height()); ul.width(ulw); ul.css("postition", "absolute"); ul.css("right", rightul); } else { var ul = document.createElement("div"); document.getElementById(id).appendChild(ul); ul.id = "ul"; ul = $("#ul"); ul.height($(window).height()); ul.width(ulw); ul.css("position", "absolute"); ul.css("right", rightul); } } resize(); //resize event $(window).bind("resize", function() { resize() }); $(document).bind("resize", function() { resize() }); var box = $("#cmsgs"); box.css("background-color", "gray"); var input = $("#input") input.css("background-color", "#0FF"); var ul = $("#ul"); ul.css("background-color", "#FF0"); ul.css("padding", 0, 0, 0, 0, 0); ul.css("margin", 0, 0, 0, 0, 0); ul.css("top", 0); box.css("padding", 0, 0, 0, 0, 0); box.css("margin", 0, 0, 0, 0, 0); } Chat("chatbox") 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="chatbox"></div> 

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

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