其他分享
首页 > 其他分享> > JS 拖动(验证码)滑块

JS 拖动(验证码)滑块

作者:互联网

核心函数:createEvent、initMouseEvent、dispatchEvent

 

直接上代码:

function slide (id, offsetX) {
    var slider = document.getElementById(id);
    var rect = slider.getBoundingClientRect(),
        x0 = rect.x || rect.left,
        y0 = rect.y || rect.top,
        x1 = x0 + offsetX, 
        y1 = y0;
    var mousedown = document.createEvent('MouseEvents');
    mousedown.initMouseEvent('mousedown', true, true, window, 0, x0, y0, x0, y0, false, false, false, false, 0, null);
    slider.dispatchEvent(mousedown);  
 
    var mousemove = document.createEvent('MouseEvents');
    mousemove.initMouseEvent('mousemove', true, true, window, 0, x1, y1, x1, y1, false, false, false, false, 0, null);
    slider.dispatchEvent(mousemove);

    var mouseout = document.createEvent('MouseEvents');
    mouseout.initMouseEvent('mouseout', true, true, window, 0, x1, y1, x1, y1, false, false, false, false, 0, null);
    slider.dispatchEvent(mouseout);
}
slide('tcaptcha_drag_thumb', 100);

 


完。

 

标签:false,滑块,slider,拖动,JS,var,y1,x1,true
来源: https://www.cnblogs.com/tujia/p/16395692.html