html - CSS 边框动画适用于整个 Div 内容

标签 html css

我有一个带边框的 div,里面有表单。我决定为边界设置动画,这样它就会跳动。我的代码:

<div class="main">
<div class="border">

-Text Here-

</div>
</div>

我的 CSS:

@keyframes pulse {
  0% { opacity: 0;  animation-timing-function: ease-in;}
  20% { opacity: .2; animation-timing-function: ease-in;}
  40% { opacity: .4; animation-timing-function: ease-in;}
  45% { opacity: .8; animation-timing-function: ease-in;}
  50%% { opacity: 1; animation-timing-function: ease-in;}
  50%% { opacity: 1; animation-timing-function: ease-out;}
  55% { opacity: .8; animation-timing-function: ease-out;}
  60% { opacity: .4; animation-timing-function: ease-out;}
  80% { opacity: .2; animation-timing-function: ease-out;}
  100% { opacity: 0; animation-timing-function: ease-out;}
}

.border {
  border-radius: 25px;
  border: 8px solid #2C3E50;
  animation-name: pulse;
  animation-duration: 4s;
  animation-iteration-count: infinite;
}  

.main{
  padding: 50px;
  width: 500px;
  height: 500px;
  margin: 0 auto;
  display: flex;
  justify-content: center;
  align-items: center;
}

问题是动画适用于 div + 边框的全部内容,我希望它只适用于边框而不是其中的内容。我知道这可能很容易解决,但我不擅长 CSS。

此外,当用户单击 div 内的任意位置时我将如何启动动画,即使在 div 外部单击时也继续播放。提前致谢。

最佳答案

不要使用会影响整个 div 的 opacity,而是使用 border-colorFiddle .

@keyframes pulse {
        0%   { border-color: rgba(0, 255, 255, 1); }
        50%  { border-color: rgba(0, 255, 255, 0); }
        100% { border-color: rgba(0, 255, 255, 1); }
}

.border {
  border-radius: 25px;
  border: 8px solid #2C3E50;
  animation: pulse 2s infinite;
}  

如果你想在点击特定的 div 时打开和关闭,你可以使用如下函数切换类:

function changeclass() {
   var blah = document.getElementById("border")
   if (blah.className=="border"){
       blah.className = "";
   }
   else{
       blah.className="border";
   }
} 

您的 html 将是:

<div class="main">
<div id="border" onclick="changeclass()">
-Text Here- 
</div>
</div>

如果你想在点击内部时打开,然后在页面上的任何地方点击时关闭,然后像这样向文档添加一个事件监听器:

document.addEventListener('click', function(e) {
    e = e || window.event;
    var target = e.target || e.srcElement;   
        if (target.className == "border"){
            target.className = "";
        }
        else{
            document.getElementById("border").className = "border";
        }
}, false);

您的 html 将是:

<div class="main">
<div id="border">
-Text Here-
</div>
</div>

不透明度是 rgba 中的第 4 个位置,因此使用 0.0-1.0 作为值。越少越透明。

关于html - CSS 边框动画适用于整个 Div 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37602845/

相关文章:

html - 为什么 row-fluid :before and row-fluid:after have a height?

html - "offset from the surrounding content"是什么意思?

javascript - 读取文本文件以在 HTML 中显示

javascript - 响应式嵌入式 youtube

css - 更改移动布局上的 Bootstrap 列/行顺序?

html - Sidebaroverlapping 在内容占位符中创建的 Div,我该如何防止它?

jquery - 如何在 HTML 页面中对特定的 div 实现滚动功能

javascript - 单击圆 Angular div 的 Angular

jquery - 背景没变

css - 透明图像未出现在菜单中