jquery - 涟漪效应继续越过圆 Angular (尤其是圆形按钮)

标签 jquery html css button material-design

我正在尝试在 CSS、HTML 和 JS 中制作具有波纹效果的 Material 按钮,但波纹效果一直在像这样覆盖按钮:
-->This is a GIF of what happens
如何消除边界外的涟漪?
我尝试使用父 div 来掩盖外面的波纹,但它也掩盖了阴影和按钮的一部分。

这是一个片段:

$(".buttons").click(function() {
  var div = $("<div/>");
  var offset = $(this).offset();
  var posx = event.pageX - offset.left;
  var posy = event.pageY - offset.top;
  div.addClass("ripple");
  div.css({
    "width": $(this).width,
    "height": $(this).height,
    "top": posy,
    "left": posx,
    "background": $(this).data("ripple_color"),
  }).appendTo($(this));
  setTimeout(function() {
    div.remove();
  }, 1500);
});
//creates a div, and gets the mouse positions. then the div gets a css class and gets inserted into the button that's clicked on. after 1.5 seconds, it is removed.
* {
  font-family: Roboto;
}


.buttons {
  background-color: transparent;
  border: none;
  position: absolute;
  text-align: center;
  color: white;
  transition-timing-function: linear;
  cursor: pointer;
  display: inline-block;
  font-size: 30px;
  font-weight: 500;
  overflow: hidden;
}
.buttons:focus {
  outline: none;
}

/*raised button (the blue one)*/
.raised_button {
  background-color: #3d81f6;
  box-shadow: 0px 4px 4px #666666;
  width: 172px;
  line-height: 72px;
  border-radius: 8px;
  transition: background-color 0.2s, box-shadow 0.2s, margin-top 0.2s;
}
.raised_button:hover {
  box-shadow: 0px 10px 24px #666666;
  margin-top: -10px;
  background-color: #326ec9;
}
.raised_button:focus {
  box-shadow: 0px 4px 18px #666666;
  margin-top: -4px;
  background-color: #326ec9;
}

/*FAB (the round red button)*/
.fab {
  height: 100px;
  background-color: #dc422f;
  transition-timing-function: linear;
  box-shadow: 0px 10px 16px #666666;
  width: 100px;
  border-radius: 100%;
  transition: background-color 0.2s, box-shadow 0.2s, margin-top 0.2s, transform 0.2s;
}
.fab:hover {
  box-shadow: 0px 24px 26px #666666;
  margin-top: -10px;
  background-color: #c43a2b;
}

/*flat button (the green one without shadows)*/
.flat_button {
  color: #4CAF50;
  padding-left: 24px;
  padding-right: 24px;
  padding-top: 16px;
  padding-bottom: 16px;
  border-radius: 8px;
  transition: background-color 0.2s linear;
}
.flat_button:hover {
  background-color: rgba(200, 230, 201, 0.7);
}
.flat_button:focus {
  background-color: #E8F5E9;
}

/*ripple div that expands and creates the effect*/
.ripple {
  position: absolute;
  border-radius: 100%;
  width: 30px;
  height: 30px;
  background: white;
  animation: ripple_animation 1.5s;
}
/*ripple animation*/
@keyframes ripple_animation {
  from {
    transform: scale(1);
    opacity: 0.4;
  }
  to {
    transform: scale(100);
    opacity: 0;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link href="https://fonts.googleapis.com/css?family=Roboto:400,300italic,300,400italic,500,500italic,700,700italic" rel="stylesheet" type="text/css">
<button class="draggable buttons raised_button">RAISED</button>
<button class="draggable buttons fab">FAB</button>
<button class="draggable buttons flat_button">FLAT</button>
<script>
  $(".draggable").draggable({
    cancel: false
  });
</script>


相关代码如下:
HTML:

<button class="draggable buttons raised_button">RAISED</button>
<button class="buttons fab">FAB</button>
<button class="draggable buttons flat_button">FLAT</button>

CSS:

.ripple {
    position: absolute;
    border-radius: 100%;
    width: 30px;
    height: 30px;
    background: white;
    animation: ripple_animation 1.5s;
}

@keyframes ripple_animation {
    from {
        transform: scale(1);
        opacity: 0.4;
    }
    to {
        transform: scale(100);
        opacity: 0;
    }
}

JS:

$(".buttons").click(function() {
    var div = $("<div/>");
    var offset = $(this).offset();
    var posx = event.pageX - offset.left;
    var posy = event.pageY - offset.top;
    div.addClass("ripple");
    div.css({
        "width": $(this).width,
        "height": $(this).height,
        "top": posy,
        "left": posx,
        "background": $(this).data("ripple_color"),
    }).appendTo($(this));
    setTimeout(function() {
        div.remove();
    }, 1500);
});

最佳答案

Google Material Design 有一个解决方案。 将以下 CSS 属性添加到您的按钮:

transform: translate3d(0, 0, 0);

问题已解决。

关于jquery - 涟漪效应继续越过圆 Angular (尤其是圆形按钮),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36049303/

相关文章:

javascript - 用按钮调用内容。 Js问题

javascript - 我可以使用 CSS direction 来定位元素的滚动条吗?

javascript - 限制angularjs中的文本框

javascript - 如何扩展文本区域以填满页面

javascript - 获取以像素为单位的其他 div 高度减去百分比的 div 高度

html - 垂直对齐一行 bootstrap 整页

javascript - 在 list-style-type 而不是 list-style-image 中添加图像

jquery对齐进度条

jquery - 如何在 jQuery 中相对于触发元素定位工具提示?

jquery ajax 休息调用 - 不支持的媒体类型