简体   繁体   English

以编程方式调用rippleJs

[英]Programmatically invoke rippleJs

Ripple.JS ( GitHub | Demo | CDN ) adds a Material style ripple to HTML Elements like this: Ripple.JSGitHub | Demo | CDN )为HTML元素添加了Material样式波纹,如下所示:

$.ripple(".btn", {
  on: 'mousedown', // The event to trigger a ripple effect
  opacity: 0.4,    // The opacity of the ripple
  color: "auto",   // Set the background color. "auto" will use the text color
  duration: 0.7,   // The duration of the ripple
  easing: 'linear' // The CSS3 easing function of the ripple
});

Q: Is there a way to programmatically invoke this? 问:是否可以通过编程方式调用此方法?

Demo in jsFiddle & Stack Snippets jsFiddle和Stack代码段中的演示

 $.ripple(".btn", { on: 'mousedown', // The event to trigger a ripple effect opacity: 0.4, // The opacity of the ripple color: "auto", // Set the background color. "auto" will use the text color duration: 0.7, // The duration of the ripple easing: 'linear' // The CSS3 easing function of the ripple }); 
 body { padding: 15px; } 
 <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css"> <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/bootswatch/3.3.7/paper/bootstrap.min.css"> <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/Ripple.js/1.2.1/ripple.css"> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Ripple.js/1.2.1/ripple.js"></script> <button class="btn btn-primary btn-lg" >Click Me</button> 

Ripples are created by the local function named Trigger inside of the library. 涟漪图是由库内部名为Trigger的局部函数创建的。 Since it's locally declared, we can't execute it directly, but we can add a custom event type in the options.on setting. 由于它是在本地声明的,因此我们无法直接执行它,但是可以在options.on设置中添加自定义事件类型。

The only road bump we'll hit is that all ripples must come from somewhere ( both intuitively and programatically ), and the trigger function will rely on getting this info from the event coordinates passed in like this ripple.js#L107 : 我们遇到的唯一障碍是,所有波动都必须来自某个地方无论是直观还是编程方式 ),并且触发功能将依赖于像涟漪.js#L107这样传入的事件坐标中获取此信息:

var x = e.pageX - $this.offset().left - $ripple.width() / 2;
var y = e.pageY - $this.offset().top - $ripple.height() / 2;

To fake this, we'll have to build and trigger our own custom jQuery Event object from scratch and then stub out the pageX and pageY properties by finding the center our our new object using $.offset , .outerWidth() , and .outerHeight() like this 要伪造这一点,我们必须从头开始构建并触发我们自己的自定义jQuery Event对象 ,然后通过使用$.offset.outerWidth().outerHeight()找到我们新对象的中心,来pageYpageXpageY属性。 .outerHeight()这样

$.ripple(".btn", {
  on: 'mousedown ripple', // The event(s) to trigger a ripple effect
});

function InvokeRipple($el) {
  var event = jQuery.Event("ripple");
  event.pageX = $el.offset().left + $el.outerWidth() / 2;
  event.pageY = $el.offset().top + $el.outerHeight() / 2;
  $($el).trigger(event)
}

Demo in jsFiddle & Stack Snippets jsFiddle和Stack代码段中的演示

 $.ripple(".btn", { on: 'mousedown ripple', // The event to trigger a ripple effect opacity: 0.4, // The opacity of the ripple color: "auto", // Set the background color. "auto" will use the text color duration: 0.7, // The duration of the ripple easing: 'linear' // The CSS3 easing function of the ripple }); $("#click").click(function() { InvokeRipple($("#info")) }); function InvokeRipple($el) { var event = jQuery.Event("ripple"); event.pageX = $el.offset().left + $el.outerWidth() / 2; event.pageY = $el.offset().top + $el.outerHeight() / 2; $($el).trigger(event) } 
 body { padding: 15px; } 
 <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css"> <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/bootswatch/3.3.7/paper/bootstrap.min.css"> <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/Ripple.js/1.2.1/ripple.css"> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Ripple.js/1.2.1/ripple.js"></script> <button class="btn btn-primary btn-lg" id="click" >Invoke Ripple on other button</button> <button class="btn btn-info btn-lg" id="info" >More Info</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM