Simulate scroll event using Javascript

后端 未结 4 1553
长情又很酷
长情又很酷 2021-01-19 11:04

I am trying to Simulate a scroll event using Javascript for Mobile Safari. I am using the following code

var evt = document.createEvent("MouseEvents"         


        
4条回答
  •  南方客
    南方客 (楼主)
    2021-01-19 12:05

    So, I know I' need to simulate it too. for me it's when you have a lightbox with a overflow box that you would need it. Just one case of many I can think of. looking to for an answer. Just thought I'd share where I'm at, thou not with the jQuery.Ui.Ipad I will Google that.. but here is what I got so far and does work but not perfectly.

        var inTouch=false;
        var timers_arr = new Array();
        var c=0;
        var t;
        var timer_is_on=0;
        //-------------------------------------------------------------------------------------------------------------------------------------------------------------
        // jeremy's timer functions
        //-------------------------------------------------------------------------------------------------------------------------------------------------------------
            function clearCount(timer){
                    /// clear the time from timer
                    clearTimeout(timers_arr[timer]);
                    /// Make sure it's clear  
                    timers_arr[''+timer+'']=0;
                    delete timers_arr[''+timer+''];
    
            }
            function setCount(timer,time,func){
                    clearCount(timer);
                    if(timers_arr[timer]==0||typeof(timers_arr[timer]) === 'undefined'){
                      timers_arr[timer]=setTimeout(function(){
                                            func();                                                 
                            },time);
                    }
            }
    
        function updatePos(evt,startY){
            setCount('touchmove',1,function(){
                var orig = evt.originalEvent;
                var curY = orig.changedTouches[0].pageY;
                var y = curY - startY;
                var sliderVal = $("#slider-vertical").slider("value");
                sliderVal += (y*.008);
                $("#slider-vertical").slider("value", sliderVal);
                updatePos(evt,startY);
            });
            setCount('touchmove_anitMotion',200,function(){
                clearCount('touchmove');
                clearCount('touchmove_anitMotion');
            });
        }
        var startX=0;
        var startY=0;
        var x=0;
        var y=0;
        var direction='';
        $('body').bind("onorientationchange", updateOrientation, false);
        $('#scroll-pane').live("touchstart", function(evt){
           inTouch=true;
           startX = event.targetTouches[0].pageX; 
           startY = event.targetTouches[0].pageY; 
        }); 
        $('#scroll-pane').live("touchmove", function(evt){
            evt.stopPropagation();
            evt.preventDefault();
            updatePos(evt,startY);
        }); 
        $('#scroll-pane').live("touchend", function(evt){
            startX=0;
            startY=0;
            clearCount('touchmove');
            inTouch=false;
        }); 
        $('#scroll-pane').live("touchcancel", function(evt){
            startX=0;
            startY=0;
            clearCount('touchmove');
            inTouch=false;
        });
    

    Again not perfect and looking to fix it.. but it's at the least working. Now note, this is a div that is using the jQuery UI slider for a scroll bar as (thou in mine it's vertical) shown in

    http://jqueryui.com/demos/slider/#side-scroll

    Hope that spurs some ideas. If I get a super stable answer I'll get back. Cheers -Jeremy

提交回复
热议问题