html - 使用 bootstrap 4 围绕一个圆对齐不同的图像并在轨道上旋转并在悬停时以圆圈显示文本

标签 html css twitter-bootstrap bootstrap-4

我正在尝试制作一个圆圈,圆圈周围会有不同的图像。并且图像将像行星一样在轨道上围绕圆圈旋转。在悬停这些图像时,我想停止旋转,并试图在圆心显示一些不同的文本。我画了一个完美的圆,但我无法将这些图像围绕圆对齐并使其围绕圆无限旋转。下图是它的一个例子。 enter image description here

所以基本上带数字的小圆圈是不同的图像,悬停在这些图像上时,一些不同的文本将显示在大圆圈的中心,旋转将停止。我也试图让它响应,所以我将它添加到一个容器中并尝试将它放在 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/

相关文章:

html - 我想在不使用 Bootstrap 的情况下对齐导航栏中的元素

javascript - 将 div 制作成链接的最佳做法是什么?

jquery - 如何在 jquery tablesorter 上动态设置行颜色

html - 粘性页脚不适用于全宽 slider

javascript - 如果不支持 javascript,bootstrap 会回退到 html 模式吗?

javascript - 从 fiddle 中获取选项卡示例并在我的元素中使用

javascript - 在 mysql 数据库中存储许多 html5 SVG 元素(路径、圆、线等)的最佳方法

html - 如何正确地将跨度集中在按钮内?

css - 防止 Bootstrap 预加载所有 hidden-* 图像

css - 一列空间太大