简体   繁体   English

单击一个按钮将一个div覆盖另一个

[英]Overlay one div over another on click of a button

I have two visualizations, each in different div. 我有两个可视化效果,每个都在不同的div中。 I want to place one div over another on button click to overlay and compare them. 我想在单击按钮时将一个div放置在另一个div上以进行比较。 Then, on another button click, I want to separate it out. 然后,在另一个按钮上单击,我想将其分开。 Any code snippet or links would be appreciated. 任何代码段或链接将不胜感激。 Right now I am just trying to make use of the following javascript function: 现在,我只是想利用以下javascript函数:

function ShowOverlay(divID, xCoordinate, yCoordinate) {    
    var divObject = document.getElementById(divID);    
    divObject.style.visibility = "visible";    
    divObject.style.left = xCoordinate;    
    divObject.style.top = yCoordinate;    
}

you should use absolute or relative positioning. 您应该使用绝对或相对定位。

If your position property is not set to absolute or relative style.left and style.top won't have any effect. 如果您的position属性未设置为absoluterelative style.leftstyle.top将不起作用。

The following will allow them to move (you just need to work out the coordinates: 以下内容将允许他们移动(您只需要计算坐标即可:

function ShowOverlay(divID, xCoordinate, yCoordinate) {

    var divObject = document.getElementById(divID);

    divObject.style.visibility = "visible";
    divObject.style.left = xCoordinate;
    divObject.style.top = yCoordinate;
    divObject.style.position = "absolute";

}

To undo it, simple set position back to static: 要撤消它,只需将位置设置回静态即可:

divObject.style.position = "static";

Here is very simple working example of moving three div's over one another on button click. 这是一个非常简单的工作示例,在单击按钮时将三个div移到另一个。 HTML code HTML代码

<div class="container">
 <div class = "circlea"><h2>link</h2></div>
 <div class = "circleb"><h2>link</h2></div>
 <div class = "circlec"><h2>link</h2></div>
 <button >click me</button>
</div>


.circlea,.circleb,.circlec{
 height:150px;
 width:150px;
 border-radius:50%;
 margin-left:50%;
 margin-top:120px;
 position: absolute;
 transition: all 1s ease-in-out;
 opacity:0;
 color:white;
 text-align:center; 
}

.circlea{
 background-color:rgba( 20, 155, 138 );
 }

.circleb{
 background-color:rgba( 155, 20, 144 );
}

.circlec{
 background-color:rgba( 24, 155, 20);
 opacity:1;
}

button{
 background-color:tomato;
 width:100px;
 padding:10px;
 color:white;
 }

.transforma{
 margin-left:200px;
 opacity:1;
 }

.transformb{
  margin-left:800px;
  opacity:1;
 }

.transformb{
 opacity:1;
 }

js js

 $("button").click(function(){

  $("div.circleb").toggleClass("transformb");
  $("div.circlea").toggleClass("transforma");

 });

https://codepen.io/pranjalkoshti/pen/rYNQeL https://codepen.io/pranjalkoshti/pen/rYNQeL

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

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