html - 为什么悬停样式在应用于我的 div 时不起作用?

标签 html css

<分区>

我正在查看网站 https://gradientbuttons.colorion.co/ .它有一些很棒的按钮和悬停示例。我尝试了他们的例子,但我无法让他们工作。这是我的:

div {
  width: 90%;
  height: 200px;
  background-image: linear-gradient(to right, #fffc00 0%, #ffffff 51%, #fffc00 100%);
  background-image: -moz-linear-gradient(to right, #fffc00 0%, #ffffff 51%, #fffc00 100%);
  background-image: -webkit-gradient(linear, left top, left right, color-stop(0%,#fffc00),color-stop(51%,#ffffff ), color-stop(100%,#fffc00 ));
  background-image: -webkit-linear-gradient(to right, #fffc00 0%, #ffffff 51%, #fffc00 100%);
  background-image: -o-linear-gradient(to right, #fffc00 0%, #ffffff 51%, #fffc00 100%);
  background-image: -ms-linear-gradient(to right, #fffc00 0%, #ffffff 51%, #fffc00 100%);
  background-image: linear-gradient(to right, #fffc00 0%, #ffffff 51%, #fffc00 100%);
  border-radius: 15px;
  position: relative;
  margin: 0 auto;
  margin-top: 15px;
  -moz-box-shadow:
    inset 0 -3em 3em rgba(0,0,0,0.1),
    0 0  0 2px rgb(255,255,255),
    0.3em 0.3em 1em rgba(0,0,0,0.3);
  -webkit-box-shadow:
    inset 0 -3em 3em rgba(0,0,0,0.1),
    0 0  0 2px rgb(255,255,255),
    0.3em 0.3em 1em rgba(0,0,0,0.3);
  box-shadow:
    inset 0 -3em 3em rgba(0,0,0,0.1),
    0 0  0 2px rgb(255,255,255),
    0.3em 0.3em 1em rgba(0,0,0,0.3);
}

div:hover {
  background-position: right center;
}
<div></div>

它不显示上述网站上显示的动画。

最佳答案

根据他们的来源,您缺少 transition: 0.5s; 因此过渡不是瞬时的,并且 background-size: 200% auto; 增加按钮的背景宽度,因此当它向右滚动时它有要显示的内容。

使用他们的源代码,这是一个简单按钮的示例:

.btn-grad {
  padding:15px;
  background-image: linear-gradient(to right, #2b5876 0%, #4e4376  51%, #2b5876  100%);
    transition: 0.5s;
    background-size: 200% auto;
    color: white;
    text-shadow: 0px 0px 10px rgba(0,0,0,0.2);
    box-shadow: 0 0 20px #eee;
    display: block;
}
.btn-grad:hover { 
  background-position: right center; 
}
<button class="btn-grad">Submit</button>

这是你的代码:

div {
  width: 90%;
  height: 200px;
  background-image: linear-gradient(to right, #fffc00 0%, #ffffff 51%, #fffc00 100%);
  background-image: -moz-linear-gradient(to right, #fffc00 0%, #ffffff 51%, #fffc00 100%);
  background-image: -webkit-gradient(linear, left top, left right, color-stop(0%,#fffc00),color-stop(51%,#ffffff ), color-stop(100%,#fffc00 ));
  background-image: -webkit-linear-gradient(to right, #fffc00 0%, #ffffff 51%, #fffc00 100%);
  background-image: -o-linear-gradient(to right, #fffc00 0%, #ffffff 51%, #fffc00 100%);
  background-image: -ms-linear-gradient(to right, #fffc00 0%, #ffffff 51%, #fffc00 100%);
  background-image: linear-gradient(to right, #fffc00 0%, #ffffff 51%, #fffc00 100%);
  border-radius: 15px;
  position: relative;
  margin: 0 auto;
  margin-top: 15px;
  -moz-box-shadow:
    inset 0 -3em 3em rgba(0,0,0,0.1),
    0 0  0 2px rgb(255,255,255),
    0.3em 0.3em 1em rgba(0,0,0,0.3);
  -webkit-box-shadow:
    inset 0 -3em 3em rgba(0,0,0,0.1),
    0 0  0 2px rgb(255,255,255),
    0.3em 0.3em 1em rgba(0,0,0,0.3);
  box-shadow:
    inset 0 -3em 3em rgba(0,0,0,0.1),
    0 0  0 2px rgb(255,255,255),
    0.3em 0.3em 1em rgba(0,0,0,0.3);
    transition: 0.5s;
    background-size: 200% auto;
}

div:hover {
  background-position: right center;
}
<div></div>

关于html - 为什么悬停样式在应用于我的 div 时不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59503271/

上一篇:html - 如何让一个 div 作为一个盒子不跨越/跨越屏幕

下一篇:css - 如何增加 scss 变量?

相关文章:

html - HTML 文档中是否有关于 Lang 属性的新规则?

html - div 未显示在网页上?

html - 如何在 HTML 中将文本和图像并排放置?

javascript - 如何使用 CSS3/HTML5 或 Javascript 根据 div 位置创建 zoom 'windows' 显示图像的缩放部分?

html - 字体未加载,但 URL 指向正确

css - TextField 两侧的边框不同

html - 如何向我的网站添加新的 flaticons?

html - 如何在 Bootstrap 中以表格形式居中输入?

javascript - 拖放不适用于溢出自动

javascript - 如果动态更改 href,则无法重定向