javascript滚动触摸滚动

发布于 2024-12-11 17:47:31 字数 5237 浏览 0 评论 0原文

我想向我的应用程序添加触摸滚动。我已经可以使用鼠标滚轮、拖动、单击等进行滚动。我(某种程度上)设法使用名为 触摸滚动

问题是,当我添加代码时,div 变得可以滚动,但又回到顶部(弹性效果)。我想知道如何才能使其正确滚动。

注意 我尝试过 iscroll-4,但它对我不起作用。

注意 此外,我正在使用 http://manos.malihu.gr/sideways-jquery-fullscreen-image-gallery

初始化插件:

<script src="https://github.com/neave/touch-scroll/raw/master/touch-scroll.min.js"></script>
<script>
    $(document).ready(function() {
        if (!!('ontouchstart' in window)) {
            $('#customScrollBox .container').touchScroll();
        }
    });
</script> 

我的 Javascript

$(window).load(function() {
    $toolbar.data("imageViewMode",$defaultViewMode); //default view mode
    ImageViewMode($toolbar.data("imageViewMode"));
    //cache vars
    $customScrollBox=$("#customScrollBox");
    $customScrollBox_container=$("#customScrollBox .container");
    $customScrollBox_content=$("#customScrollBox .content");
    $dragger_container=$("#dragger_container");
    $dragger=$("#dragger");

    CustomScroller();

    function CustomScroller(){
        outerMargin=0;
        innerMargin=20;
        $customScrollBox.height($(window).height()-outerMargin);
        $dragger_container.height($(window).height()-innerMargin);
        visibleHeight=$(window).height()-outerMargin;
        if($customScrollBox_container.height()>visibleHeight){ //custom scroll depends on content height
            $dragger_container,$dragger.css("display","block");
            totalContent=$customScrollBox_content.height();
            draggerContainerHeight=$(window).height()-innerMargin;
            animSpeed=400; //animation speed
            easeType="easeOutCirc"; //easing type
            bottomSpace=1.05; //bottom scrolling space
            targY=0;
            draggerHeight=$dragger.height();
            $dragger.draggable({ 
                axis: "y", 
                containment: "parent", 
                drag: function(event, ui) {
                    Scroll();
                }, 
                stop: function(event, ui) {
                    DraggerOut();
                }
            });

            //scrollbar click
            $dragger_container.click(function(e) {
                var mouseCoord=(e.pageY - $(this).offset().top);
                var targetPos=mouseCoord+$dragger.height();
                if(targetPos<draggerContainerHeight){
                    $dragger.css("top",mouseCoord);
                    Scroll();
                } else {
                    $dragger.css("top",draggerContainerHeight-$dragger.height());
                    Scroll();
                }
            });

            //mousewheel
            $(function($) {
                $customScrollBox.bind("mousewheel", function(event, delta) {
                    vel = Math.abs(delta*10);
                    $dragger.css("top", $dragger.position().top-(delta*vel));
                    Scroll();
                    if($dragger.position().top<0){
                        $dragger.css("top", 0);
                        $customScrollBox_container.stop();
                        Scroll();
                    }
                    if($dragger.position().top>draggerContainerHeight-$dragger.height()){
                        $dragger.css("top", draggerContainerHeight-$dragger.height());
                        $customScrollBox_container.stop();
                        Scroll();
                    }
                    return false;
                });
            });

            function Scroll(){
                var scrollAmount=(totalContent-(visibleHeight/bottomSpace))/(draggerContainerHeight-draggerHeight);
                var draggerY=$dragger.position().top;
                targY=-draggerY*scrollAmount;
                var thePos=$customScrollBox_container.position().top-targY;
                $customScrollBox_container.stop().animate({top: "-="+thePos}, animSpeed, easeType); //with easing
            }

            //dragger hover
            $dragger.mouseup(function(){
                DraggerOut();
            }).mousedown(function(){
                DraggerOver();
            });

            function DraggerOver(){
                $dragger.css("background", "url(round_custom_scrollbar_bg_over.png)");
            }

            function DraggerOut(){
                $dragger.css("background", "url(round_custom_scrollbar_bg.png)");
            }
        } else { //hide custom scrollbar if content is short
            $dragger,$dragger_container.css("display","none");
        }
    }

    //resize browser window functions
    $(window).resize(function() {
        FullScreenBackground("#bgimg"); //scale bg image
        $dragger.css("top",0); //reset content scroll
        $customScrollBox_container.css("top",0);
        $customScrollBox.unbind("mousewheel");
        CustomScroller();
    });

    LargeImageLoad($bgimg);
});

I want to add touch scrolling to my app. I have already got scrolling with the mouse wheel, drag, click, etc. I have (sort of) managed to get the scrolling to work on iPad using a plugin called touch-scroll.

The problem is that when I add the code, the div becomes sort of scrollable, but goes back to the top (elastic effect). I am wondering how can I make it scroll properly.

Note I have tried iscroll-4 but it did not work for me.

Note Also I am using the website template from http://manos.malihu.gr/sideways-jquery-fullscreen-image-gallery.

Initializing the plugin:

<script src="https://github.com/neave/touch-scroll/raw/master/touch-scroll.min.js"></script>
<script>
    $(document).ready(function() {
        if (!!('ontouchstart' in window)) {
            $('#customScrollBox .container').touchScroll();
        }
    });
</script> 

My Javascript

$(window).load(function() {
    $toolbar.data("imageViewMode",$defaultViewMode); //default view mode
    ImageViewMode($toolbar.data("imageViewMode"));
    //cache vars
    $customScrollBox=$("#customScrollBox");
    $customScrollBox_container=$("#customScrollBox .container");
    $customScrollBox_content=$("#customScrollBox .content");
    $dragger_container=$("#dragger_container");
    $dragger=$("#dragger");

    CustomScroller();

    function CustomScroller(){
        outerMargin=0;
        innerMargin=20;
        $customScrollBox.height($(window).height()-outerMargin);
        $dragger_container.height($(window).height()-innerMargin);
        visibleHeight=$(window).height()-outerMargin;
        if($customScrollBox_container.height()>visibleHeight){ //custom scroll depends on content height
            $dragger_container,$dragger.css("display","block");
            totalContent=$customScrollBox_content.height();
            draggerContainerHeight=$(window).height()-innerMargin;
            animSpeed=400; //animation speed
            easeType="easeOutCirc"; //easing type
            bottomSpace=1.05; //bottom scrolling space
            targY=0;
            draggerHeight=$dragger.height();
            $dragger.draggable({ 
                axis: "y", 
                containment: "parent", 
                drag: function(event, ui) {
                    Scroll();
                }, 
                stop: function(event, ui) {
                    DraggerOut();
                }
            });

            //scrollbar click
            $dragger_container.click(function(e) {
                var mouseCoord=(e.pageY - $(this).offset().top);
                var targetPos=mouseCoord+$dragger.height();
                if(targetPos<draggerContainerHeight){
                    $dragger.css("top",mouseCoord);
                    Scroll();
                } else {
                    $dragger.css("top",draggerContainerHeight-$dragger.height());
                    Scroll();
                }
            });

            //mousewheel
            $(function($) {
                $customScrollBox.bind("mousewheel", function(event, delta) {
                    vel = Math.abs(delta*10);
                    $dragger.css("top", $dragger.position().top-(delta*vel));
                    Scroll();
                    if($dragger.position().top<0){
                        $dragger.css("top", 0);
                        $customScrollBox_container.stop();
                        Scroll();
                    }
                    if($dragger.position().top>draggerContainerHeight-$dragger.height()){
                        $dragger.css("top", draggerContainerHeight-$dragger.height());
                        $customScrollBox_container.stop();
                        Scroll();
                    }
                    return false;
                });
            });

            function Scroll(){
                var scrollAmount=(totalContent-(visibleHeight/bottomSpace))/(draggerContainerHeight-draggerHeight);
                var draggerY=$dragger.position().top;
                targY=-draggerY*scrollAmount;
                var thePos=$customScrollBox_container.position().top-targY;
                $customScrollBox_container.stop().animate({top: "-="+thePos}, animSpeed, easeType); //with easing
            }

            //dragger hover
            $dragger.mouseup(function(){
                DraggerOut();
            }).mousedown(function(){
                DraggerOver();
            });

            function DraggerOver(){
                $dragger.css("background", "url(round_custom_scrollbar_bg_over.png)");
            }

            function DraggerOut(){
                $dragger.css("background", "url(round_custom_scrollbar_bg.png)");
            }
        } else { //hide custom scrollbar if content is short
            $dragger,$dragger_container.css("display","none");
        }
    }

    //resize browser window functions
    $(window).resize(function() {
        FullScreenBackground("#bgimg"); //scale bg image
        $dragger.css("top",0); //reset content scroll
        $customScrollBox_container.css("top",0);
        $customScrollBox.unbind("mousewheel");
        CustomScroller();
    });

    LargeImageLoad($bgimg);
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乜一 2024-12-18 17:47:31

您是否尝试过使用纯 css 解决方案? iOS 设备不会显示滚动条,因此您可以将可滚动元素的 css 更新为:

overflow-y: scroll;
-webkit-overflow-scrolling: touch;

您可以调整可滚动元素的高度来调整可滚动区域

Have you tried going with a pure css solution? iOS devices will not show scrollbars so you can update the css for your scrollable element to:

overflow-y: scroll;
-webkit-overflow-scrolling: touch;

You can adjust the height of the scrollable element to adjust the scrollable area

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文