[英]how would i pass the element as a parameter through the nested function?
I'm trying to pass the selector as a parameter through the Slider
to an Object nested function. 我试图通过Slider
将选择器作为参数传递给Object嵌套函数。
I expected that istouchEnd
could get the .storage1
as a parameter form the Slider
but this.storage
never receives the parameter from the Slider
function. 我希望istouchEnd
可以将.storage1
作为Slider
的参数,但是this.storage
永远不会从Slider
函数接收参数。
Is there any ways to pass(or get whatever) the span
tag through the Slider
function as a parameter? 有没有办法通过Slider
函数作为参数传递(或得到任何) span
标签?
'use strict'; const blueMethods = { count: 1, istouchStart(e) { this.coordX = e.touches[0].clientX; return this.coordX; }, istouchMove(e) { let drag = e.touches[0].clientX; let dist = Math.sqrt(drag + this.coordX); }, istouchEnd(e) { let dist = e.changedTouches[0].clientX - this.coordX; $(this.storage).text(dist); } } function Slider(target, storage) { Slider.target = target; Slider.storage = storage; $(Slider.target).stop(true, true).on({ touchstart:blueMethods.istouchStart, touchmove:blueMethods.istouchMove, touchend:blueMethods.istouchEnd }); }; const box1 = Slider('.page1', '.storage1'); box1;
* { margin: 0; padding: 0; } .box { width: 100%; height: 70%; float: left; } .page1 { width: 48vw; height: 80vh; background-color: lightblue; float: left; } .page2 { width: 48vw; height: 80vh; background-color: orange; float: left; }
<div class="box"> <div class="page1"></div> <div class="page2"></div> </div> <div class="textbox"> <span class="storage1"></span> <span class="storage2" data-count></span> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
When you set event handlers which are methods in some object and want to pass some parameters and also give them acces this
you can create handlers in following way - eg for function istouchEnd
当您设置事件处理程序,其在某些对象的方法,并希望通过一些参数,也给他们存取权限this
您可以创建以下方式处理-例如用于功能istouchEnd
istouchEnd(storage) {
return (e) => {
let dist = e.changedTouches[0].clientX - this.coordX;
$(storage).text(dist);
}
}
As you see the istouchEnd
is not handler, but it create and return handler (using arrow function ) which has access to this.coordX
and storage parameter. 如您所见, istouchEnd
不是处理程序,但它创建并返回处理程序(使用箭头函数 ),该处理程序可以访问this.coordX
和存储参数。 You use it by change touchend:blueMethods.istouchEnd
to 你通过改变touchend:blueMethods.istouchEnd
使用它touchend:blueMethods.istouchEnd
to
touchend:blueMethods.istouchEnd(storage)
'use strict'; const blueMethods = { count: 1, istouchStart() { return (e) => { this.coordX = e.touches[0].clientX; return this.coordX; } }, istouchMove() { return (e) => { let drag = e.touches[0].clientX; let dist = Math.sqrt(drag + this.coordX); } }, istouchEnd(storage) { return (e) => { let dist = e.changedTouches[0].clientX - this.coordX; $(storage).text(dist); } } } function Slider(target, storage) { Slider.target = target; Slider.storage = storage; $(Slider.target).stop(true, true).on({ touchstart:blueMethods.istouchStart(), touchmove:blueMethods.istouchMove(), touchend:blueMethods.istouchEnd(storage) }); }; const box1 = Slider('.page1', '.storage1'); box1;
* { margin: 0; padding: 0; } .box { width: 100%; height: 70%; float: left; } .page1 { width: 48vw; height: 80vh; background-color: lightblue; float: left; } .page2 { width: 48vw; height: 80vh; background-color: orange; float: left; }
<div class="box"> <div class="page1"></div> <div class="page2"></div> </div> <div class="textbox"> <span class="storage1"></span> <span class="storage2" data-count></span> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.