简体   繁体   English

在屏幕外添加/添加div?

[英]Prepend/Append a div offscreen?

I'm trying to create a div that has two buttons: one to prepend a div of the same size to the right of the original without shifting the position of the original div and another button to append a div to the right of the original. 我正在尝试创建一个具有两个按钮的div:一个在原始尺寸的右边添加一个相同大小的div, 而不移动原始div的位置,另一个按钮在原始尺寸的右边附加一个div。

Appending to the left works fine, but prepending left shifts the whole container over shown here 向左追加效果很好,但向左追加则将整个容器移到此处所示的位置

code I'm using to add elements: 我用来添加元素的代码:

$(document).ready(function() {
  $("#back").on("click", function(e) {
    e.preventDefault();
    $("#center").before("<div id='back' class='inside'></div>");
  });
  $("#next").on("click", function(e) {
    e.preventDefault();
    $("#center").after("<div id='next' class='inside'></div>");
  });
});

How do I prepend the div left of the original so that it is "off screen" without shifting the position of the original div? 如何在原始div的左侧添加前缀,使其处于“屏幕外”状态而又不移动原始div的位置? Thanks in advance! 提前致谢!

The solution to your problem is best solved using CSS. 使用CSS可以最好地解决您的问题。

Simply set display:none to the left DIV upon initial insertion. 只需在初始插入时将display:none设置为左侧DIV。

Change this: 更改此:

$("#center").before("<div id='back' class='inside'></div>");

To this: 对此:

$("#center").before("<div id='back' class='inside' style='display:none;'></div>");

And remove that CSS attribute when you want it visible: 并在需要时删除该CSS属性:

$('#back').css('display', '');

However, the use of inline styling is, in most cases, bad practice. 但是,在大多数情况下,使用内联样式是不好的做法。 You should consider setting the display:none via a stylesheet assigned to the element ID #back . 您应该考虑通过分配给元素ID #back的样式表设置display:none For maintainable code, you want to keep clear separation of style (CSS), structure (HTML) and UI functionality (JS). 对于可维护的代码,您希望保持样式(CSS),结构(HTML)和UI功能(JS)的清晰区分。

try to set the following CSS for the containing div 尝试为包含的div设置以下CSS

div.whatever {
position:absolute;
overflow:hidden;
}

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

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