gpt4 book ai didi

javascript - 从 jQuery 到 AngularJS 的 Material Design 涟漪

转载 作者:行者123 更新时间:2023-11-30 10:04:26 25 4
gpt4 key购买 nike

我有一些简单的 CSS Material 设计波纹效果代码,只需在其上应用 .ripple 类(但它必须具有宽度和高度)即可用于每个需要的元素。代码如下:

(function(window, $) {
$(function() {
$('.ripple').on('click', function(event) {
event.preventDefault();

var $div = $('<div/>'),
btnOffset = $(this).offset(),
xPos = event.pageX - btnOffset.left,
yPos = event.pageY - btnOffset.top;

$div.addClass('ripple-effect');
var $ripple = $(".ripple-effect");

$ripple.css("height", $(this).height());
$ripple.css("width", $(this).height());
$div.css({
top: yPos - ($ripple.height() / 2),
left: xPos - ($ripple.width() / 2),
background: $(this).data("ripple-color")
})
.appendTo($(this));

window.setTimeout(function() {
$div.remove();
}, 2000);
});
});
})(window, jQuery);
body {
text-align: center;
}
button {
position: relative;
border: none;
outline: none;
cursor: pointer;
background: #89669b;
color: white;
padding: 18px 60px;
border-radius: 2px;
font-size: 22px;
}
.fab {
border-radius: 50%;
margin: 0;
padding: 20px;
}
.material {
position: relative;
color: white;
margin: 20px auto;
height: 400px;
width: 500px;
background: #f45673;
}
.ripple {
overflow: hidden;
}
.ripple-effect {
position: absolute;
border-radius: 50%;
width: 50px;
height: 50px;
background: white;
animation: ripple-animation 2s;
}
@keyframes ripple-animation {
from {
transform: scale(1);
opacity: 0.4;
}
to {
transform: scale(100);
opacity: 0;
}
}
<!DOCTYPE html>
<html lang="en">

<head itemscope itemtype="http://schema.org/WebSite">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Material Design Ripple</title>
</head>

<body>
<h1>Material Design Buttons</h1>
<h3>Just add the class ripple to anything and use the "data-ripple-color" property to set the ripple color</h3>

<button class="ripple">Login</button>
<button class="ripple" data-ripple-color="#89669b" style="background:white; color:black;">Login</button>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</body>

</html>

我的问题是:您可以将 JavaScript 函数代码更改为 AngularJS 以便不再使用 jQuery 并获得相同的结果吗?目的是在主元素中使用它。

提前谢谢你。

最佳答案

您可以在指令中编写这段代码,基本上它会为您执行 DOM 操作。只要找到 class="ripple"

,该指令就会开始工作

指令

app.directive('ripple', function($timeout) {
return {
restrict: 'C',
link: function(scope, element, attrs) {
element.on('click', function(event) {
event.preventDefault();

var $div = angular.element('<div></div>'),
btnOffset = $(this).offset(),
xPos = event.pageX - btnOffset.left,
yPos = event.pageY - btnOffset.top;

$div.addClass('ripple-effect');
var $ripple = angular.element(".ripple-effect");

$ripple.css("height", $(this).height());
$ripple.css("width", $(this).height());
$div.css({
top: yPos - ($ripple.height() / 2),
left: xPos - ($ripple.width() / 2),
background: $(this).data("ripple-color")
})
.appendTo($(this));

$timeout(function() {
$div.remove();
}, 2000);
});
}
}

});

关于javascript - 从 jQuery 到 AngularJS 的 Material Design 涟漪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29988477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com