首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从jQuery到AngularJS的材料设计波纹

从jQuery到AngularJS的材料设计波纹
EN

Stack Overflow用户
提问于 2015-05-01 14:11:32
回答 1查看 1.4K关注 0票数 1

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

代码语言:javascript
复制
(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);
代码语言:javascript
复制
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;
  }
}
代码语言:javascript
复制
<!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>

,我的问题是:,您能按照不再使用jQuery的顺序将JavaScript函数代码更改为AngularJS,并得到相同的结果吗?其目的是在一个主要项目中使用它。

提前谢谢你。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-01 14:36:11

您可以在指令中编写此代码,基本上这将为您执行DOM操作。只要找到class="ripple",这个指令就会开始工作

指令

代码语言:javascript
复制
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);
            });
        }
    }

});
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29988477

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档