javascript - 动态填充两个对象之间的空间
问题描述
填充这两个对象之间的空间的最佳方法是什么,本质上是制作一个对象来填充从 a 到 b 的最右边的空间。我需要一个动态的解决方案,因为 A 和 B 的位置会有所不同,有时 B 会比 A 更左。我希望它们从两个单独的对象开始。还要注意我只希望填充两者之间的空间,外面什么都没有。
function myFunction() {
var x = document.getElementById('a');
var y = document.getElementById('b');
x.style.right = "70%";
y.style.right = "50%";
var greenRect = x.getBoundingClientRect();
var blueRect = y.getBoundingClientRect();
}
h1 {
position: relative;
width: auto;
height: 50px;
background-color: beige;
}
h2 {
position: relative;
}
#a {
position: relative;
width: 50px;
height: 50px;
background-color: green;
float: right;
margin-left: -50px;
transform-origin: left;
border-radius: 50%;
}
#b {
position: relative;
width: 50px;
height: 50px;
background-color: green;
float: right;
border-radius: 50%;
}
<h1>
<div id='a'></div>
<div id='b'></div>
</h1>
<h2>
<button onclick='myFunction()'>PRESS</button>
</h2>
解决方案
您可以使用一些背景和框阴影技巧在视觉上填充以下之间的空间:
var x = document.getElementById('a');
var y = document.getElementById('b');
function myFunction() {
var r = Math.random()*100;
y.style.right=r+"%";
x.style.right=(Math.random()*(100 - r) + r)+"%";
}
h1 {
overflow:auto;
background-color: blue;
color:#fff;
text-align:center;
}
#a {
position: relative;
width: 50px;
height: 50px;
background-color: green;
float: right;
margin-left: -50px;
transform-origin: left;
border-radius: 50%;
box-shadow:-50vw 0 0 50vw beige;
}
#b {
position: relative;
width: 50px;
height: 50px;
background-color: green;
float: right;
border-radius: 50%;
box-shadow:50vw 0 0 50vw beige;
}
<h1>
<div id='a'>1</div>
<div id='b'>2</div>
</h1>
<h2>
<button onclick='myFunction()'>PRESS</button>
</h2>
推荐阅读
- microsoft-teams - 在 IOS 的任务模块窗口中选择文件时,Microsoft Teams 崩溃
- c++ - 使用模板元编程优化结构序列化
- eleventy - 十一:输出集合中的数据子集
- javascript - 如何在 Puppeteer page.$eval 中传递函数/变量?
- node.js - 从子目录访问配置模块
- python - 在 tkinter python 中同时绑定到 2 个事件
- javascript - props vue js的问题。第二个元素不起作用
- flutter - Flutter - Draggable 小部件的反馈没有正确动画
- google-apps-script - GCalendar 在我的完整日历/完整日历中搜索
- laravel - Laravel刀片模板:检查记录存在于数据透视表中