我正在尝试制作一个圆圈,圆圈周围会有不同的图像。并且图像将像行星一样在轨道上围绕圆圈旋转。在悬停这些图像时,我想停止旋转,并试图在圆心显示一些不同的文本。我画了一个完美的圆,但我无法将这些图像围绕圆对齐并使其围绕圆无限旋转。下图是它的一个例子。
所以基本上带数字的小圆圈是不同的图像,悬停在这些图像上时,一些不同的文本将显示在大圆圈的中心,旋转将停止。我也试图让它响应,所以我将它添加到一个容器中并尝试将它放在 col-3 中。
我在对齐这些图像以及在圆心显示文本时遇到问题。到目前为止,这是我尝试过的 HTML
<div class="col-3 mx-auto" style="background-color: grey;">
<img src="images/location/2x/location.png" style="float: left; ">
<div class="circle">
<div class="text-center">
</div>
</div>
</div>
CSS
.circle{
width: 80%;
height:0;
padding-bottom: 80%;
-moz-border-radius: 50%;
-webkit-border-radius: 50%;
border-radius: 50%;
background: #4679BD;
}
实现此目标的更好方法是什么?
有什么帮助吗?
最佳答案
这是我的解决方案, 它并不完美,但它会给你一个起点。
动态文本可以从data-text
属性传递
<img data-text="A" src="http://placehold.it/50x50" alt="">
为了获取此文本,我为每个图像添加了 mouseover
事件监听器。当我们将鼠标悬停在图像上时,使用
data-text
属性中获取并更新文本
sampleText.innerHTML=this.getAttribute('data-text');
要在悬停时停止动画,我使用
暂停动画.image-holder:hover {
animation-play-state: paused;
}
var images = document.getElementsByTagName('img');
var sampleText = document.getElementById('sample-text');
for (var i = 0; i < images.length; i++) {
images[i].addEventListener("mouseover", updateName)
}
function updateName() {
sampleText.innerHTML = this.getAttribute('data-text');
}
body {
padding: 100px;
}
.parent {
position: relative;
}
.circle {
width: 200px;
height: 200px;
border-radius: 50%;
background-color: teal;
display: flex;
justify-content: center;
align-items: center;
}
* {
box-sizing: border-box;
}
.image-holder {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
/*animation: rotate linear 5s infinite forwards;*/
animation-name: rotate;
animation-duration: 5s;
animation-iteration-count: infinite;
animation-timing-function: linear;
animation-fill-mode: forwards;
}
.image-holder:hover {
animation-play-state: paused;
}
.image-holder img {
position: absolute;
width: 50px;
height: 50px;
border-radius: 50%;
}
.image-holder img:nth-child(1) {
top: -15px;
left: -15px;
}
.image-holder img:nth-child(3) {
top: -15px;
right: -15px;
}
.image-holder img:nth-child(5) {
bottom: -15px;
right: -15px;
}
.image-holder img:nth-child(7) {
bottom: -15px;
left: -15px;
}
.image-holder img:nth-child(2) {
top: -50px;
left: 50%;
transform: translateX(-50%);
}
.image-holder img:nth-child(4) {
top: 50%;
right: -50px;
transform: translateY(-50%);
}
.image-holder img:nth-child(6) {
bottom: -50px;
left: 50%;
transform: translateX(-50%);
}
.image-holder img:nth-child(8) {
top: 50%;
left: -50px;
transform: translateY(-50%);
}
@keyframes rotate {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row">
<div class="parent">
<div class="circle" id="sample-text">
Sample Text
</div>
<div class="image-holder">
<img data-text="A" src="http://placehold.it/50x50" alt="">
<img data-text="B" src="http://placehold.it/50x50" alt="">
<img data-text="C" src="http://placehold.it/50x50" alt="">
<img data-text="D" src="http://placehold.it/50x50" alt="">
<img data-text="E" src="http://placehold.it/50x50" alt="">
<img data-text="F" src="http://placehold.it/50x50" alt="">
<img data-text="G" src="http://placehold.it/50x50" alt="">
<img data-text="H" src="http://placehold.it/50x50" alt="">
</div>
</div>
</div>
</div>
关于html - 使用 bootstrap 4 围绕一个圆对齐不同的图像并在轨道上旋转并在悬停时以圆圈显示文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50352950/