javascript - 如何为 div 内的 span 元素添加摇动动画

标签 javascript html css

 <div id="about-desc">
    <p>
 Lorem ipsum dolor sit <span class="animation">amet</span>, consectetur adipisicing elit. <span class="animation"> Officia reprehenderit</span>, <span class="animation"> sit eligendi</span> deserunt, blanditiis quas porro omnis provident quidem voluptate! Fugit ipsa mollitia, atque commodi asperiores, rerum dicta ratione aut.
    </p>
 </div>

$(document).ready(function () { 
  $(".animation").addClass("animated shake");
});

我无法弄清楚我到底哪里出错了。当我将动画添加到 div 元素时它工作正常但它没有发生在 span 元素上?谁能解释一下如何解决这个问题

最佳答案

span 默认是一个内联元素,因此不能进行转换。添加display: inline-block添加到您的动画或添加到跨度,使其可变形。

transformable element

A transformable element is an element in one of these categories:

an element whose layout is governed by the CSS box model which is either a block-level or atomic inline-level element, or whose display property computes to table-row, table-row-group, table-header-group, table-footer-group, table-cell, or table-caption [CSS21]

an element in the SVG namespace and not governed by the CSS box model which has the attributes transform, patternTransform or gradientTransform [SVG11].

REF: https://drafts.csswg.org/css-transforms-1/#terminology

The HTML <span> element is a generic inline container for phrasing content, which does not inherently represent anything. It can be used to group elements for styling purposes (using the class or id attributes), or because they share attribute values, such as lang. It should be used only when no other semantic element is appropriate. <span> is very much like a <div> element, but <div> is a block-level element whereas a <span> is an inline element.

REF: https://developer.mozilla.org/en/docs/Web/HTML/Element/span

选项 1:悬停时摇动

$(document).ready(function () { 
  $(".animation").addClass("animated shake");
});
.shake:hover {
  animation: shake 0.82s cubic-bezier(.36, .07, .19, .97) both;
  transform: translate3d(0, 0, 0);
  backface-visibility: hidden;
  perspective: 1000px;
  display: inline-block;
}

@keyframes shake {
  10%,
  90% {
    transform: translate3d(-1px, 0, 0);
  }
  20%,
  80% {
    transform: translate3d(2px, 0, 0);
  }
  30%,
  50%,
  70% {
    transform: translate3d(-4px, 0, 0);
  }
  40%,
  60% {
    transform: translate3d(4px, 0, 0);
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="about-desc">
    <p>
 Lorem ipsum dolor sit <span class="animation">amet</span>, consectetur adipisicing elit. <span class="animation"> Officia reprehenderit</span>, <span class="animation"> sit eligendi</span> deserunt, blanditiis quas porro omnis provident quidem voluptate! Fugit ipsa mollitia, atque commodi asperiores, rerum dicta ratione aut.
    </p>
 </div>

选项 2:永远摇动

$(document).ready(function () { 
  $(".animation").addClass("animated shake");
});
.shake {
  animation: shake 0.82s cubic-bezier(.36, .07, .19, .97) both infinite;
  transform: translate3d(0, 0, 0);
  backface-visibility: hidden;
  perspective: 1000px;
  display: inline-block;
}

@keyframes shake {
  10%,
  90% {
    transform: translate3d(-1px, 0, 0);
  }
  20%,
  80% {
    transform: translate3d(2px, 0, 0);
  }
  30%,
  50%,
  70% {
    transform: translate3d(-4px, 0, 0);
  }
  40%,
  60% {
    transform: translate3d(4px, 0, 0);
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="about-desc">
    <p>
 Lorem ipsum dolor sit <span class="animation">amet</span>, consectetur adipisicing elit. <span class="animation"> Officia reprehenderit</span>, <span class="animation"> sit eligendi</span> deserunt, blanditiis quas porro omnis provident quidem voluptate! Fugit ipsa mollitia, atque commodi asperiores, rerum dicta ratione aut.
    </p>
 </div>

关于javascript - 如何为 div 内的 span 元素添加摇动动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44355470/

相关文章:

javascript - 获取 DOM 元素的 dojo 方法

javascript - 简单有效的正则表达式不匹配

javascript - 在nodejs中使用axios检查url重定向

html - Chrome Extension - 如何设置 Extension Popup 的列宽

css - Firefox/IE 的间隙/边距问题

javascript - 在 foreach Javascript 中找到最大的数值?

css调试,跨浏览器兼容

javascript - 范围变量 ng-bind-html 未及时加载

html - 无法使图标留在输入内

javascript - 如何在服务器处理时禁用所有内容、div 等