【发布时间】:2016-10-27 17:24:02
【问题描述】:
var top= e.pageY - rect.top -ipple.offsetHeight/2 - document.body.scrollTop;
谁能解释一下上述语句的每个部分是做什么的?
'e.target' 是触发事件的对象,而涟漪是包含涟漪类的跨度,因此,如果它是涟漪本身。所以它正在寻找它相对于页面顶部的位置,为什么它同时引用rect和ripple?
这是一个更好的函数上下文:
if(!ripple){
ripple=document.createElment('span'); /*empty object, span to hold the ripple class*/
ripple.className='ripple';/*add ripple class to it (span)*/
/*place the ripple on top of the button*/
ripple.style.height=ripple.style.width=
Math.max(rect.width, rect.height)+'px';
/*set the ripple to the buttons child= attach*/
target.appendChild(ripple);
}
/*--------- DONT ANIMATE FOR NOW --------------*/
ripple.classList.remove('show');
/* complex part*/
var top= e.pageY - rect.top - ripple.offsetHeight/2 - document.body.scrollTop; /*DEBUG HERE*/
var left =e.pageX - rect.left - ripple.offsetWidth/2 - document.body.scrollLeft;
ripple.style.top= top + 'px';
ripple.style.left= left + 'px';
【问题讨论】:
-
嘿 El.oz,我无法理解的是,我知道它创建了一个跨度,然后附加创建效果的类,按钮监听点击并获取位置包含效果的跨度是通过获取 event.pageY 和页面 X 获得的点击,那么为什么脚本从 'rect.top' 获取 (-) 然后再次从ripple.offsetHeight 获取?
标签: javascript html css effect ripple