在固定按钮上拖动滚动

时间:2016-10-26 03:57:38

标签: javascript css mobile scroll

如果你看这里:

https://jsfiddle.net/8s8uamhq/1/

你会看到一堆蓝色和绿色的盒子。您可以使用窗口上的普通滚动条在此页面上向上和向左,向上和向下滚动。

我想更改它,以便如果您单击并按住右上角的箭头键,则可以滚动该方向,并且当您滚动箭头键时仍然保持原位。

目的是将其添加到网络应用程序中,以便可以在平板电脑上使用此按钮进行滚动,同时触摸屏幕可用于其他内容。

js小提琴页面的完整html如下所示:

<html>
  <head>

    <title></title>
    <meta content="">

    <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

    <link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css'>

    <style>
    #movearrow {
        position: fixed;
        top: 45px;
        right: 20px;
        z-index:99999;
        font-size: 2.5em;
        text-shadow: 0px 0 white, 0 2px white, 0px 0 white, 0 -2px white;
        cursor: grab; cursor : -o-grab; cursor : -moz-grab; cursor : -webkit-grab;
    }
    .blue, .green {
        height: 200px;
        width: 200px;
    }
    .blue {
        background-color: blue;
    }
    .green {
        background-color: green;
    }
    .column {
        float: left;
    }
    div {
        display: inline-block;
        white-space: nowrap;
    }
    </style>
  </head>

  <body>

  <span id="movearrow" class="dragscroll glyphicon glyphicon-move"></span>

  <div class=column>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
  </div>

  <div class=column>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
  </div>


  <div class=column>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
  </div>

  <div class=column>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
  </div>

  <div class=column>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
  </div>

  <div class=column>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
  </div>

  <div class=column>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
  </div>

  <div class=column>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
  </div>

  <div class=column>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
  </div>

  <div class=column>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
  </div>

  <div class=column>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
  </div>

  <div class=column>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
    <div class=blue > </div>
    <div class=green > </div>
  </div>

  </body>

</html>

1 个答案:

答案 0 :(得分:0)

要在click / mousedown / touch上触发事件,您将需要使用jQuery或JavaScript。我已经将jQuery 2.1.3用于我的解决方案,但你也可以在Pure JS中编写它。

我还在#movearrow范围内插入了虚假链接,以便我们可以在单击顶部箭头时向上滚动页面,在单击向左箭头时向左滚动,等等。

HTML:

<span id="movearrow" class="glyphicon glyphicon-move">
    <a href="#" id="left"></a>
    <a href="#" id="top"></a>
    <a href="#" id="right"></a>
    <a href="#" id="bottom"></a>
</span>

CSS:

#movearrow a {
    position: absolute;
    width: 50%;
    height: 50%;
    z-index: 9999999;
    display: block;
}

#movearrow a#left {
    left: 0;
    top: 25%;
    bottom: 25%;
    right: 50%;
}

#movearrow a#right {
    right: 0;
    top: 25%;
    bottom: 25%;
    left: 50%;
}

#movearrow a#bottom {
    bottom: 0;
    top: 50%;
    left: 25%;
    right: 25%;
}

#movearrow a#top {
    top: 0;
    bottom: 50%;
    left: 25%;
    right: 25%;
}

jQuery的:

$(document).ready(function() {
    var $top = $("#top"),
        $left = $("#left"),
        $right = $("#right"),
        $bottom = $("#bottom");
    var scrollPxW = $(".column").children().first().outerWidth(); // horizontal amount scrolled
    var scrollPxH = $(".column").children().first().outerHeight(); // vertical amount scrolled
    $("#movearrow").children("a").click(function() {
        return false; // removes function of anchor link
    });
    $bottom.on({ // for the bottom arrow
        'mousedown touchstart': function() {
        // animate scrollTop position of body & html
            $("html, body").animate({
                scrollTop: "+=" + scrollPxH + "px"
            });
        },
        'mouseup touchend': function() {
        // when hold event finishes, stop animation
            $("html, body").stop(true);
        }
    });
    $top.on({
        'mousedown touchstart': function() {
        // animate scrollTop position of body & html
            $("html, body").animate({
                scrollTop: "-=" + scrollPxH + "px"
            });
        },
        'mouseup touchend': function() {
        // when hold event finishes, stop animation
            $("html, body").stop(true);
        }
    });
    $right.on({
        'mousedown touchstart': function() {
        // animate scrollLeft position of body & html
            $("html, body").animate({
                scrollLeft: "+=" + scrollPxW + "px"
            });
        },
        'mouseup touchend': function() {
        // when hold event finishes, stop animation
            $("html, body").stop(true);
        }
    });
    $left.on({
        'mousedown touchstart': function() {
        // animate scrollLeft position of body & html
            $("html, body").animate({
                scrollLeft: "-=" + scrollPxW + "px"
            });
        },
        'mouseup touchend': function() {
        // when hold event finishes, stop animation
            $("html, body").stop(true);
        }
    });
});

jsFiddle:https://jsfiddle.net/8s8uamhq/1/

如果您想点击而不是点击+保持,您可以使用点击事件,例如:

$top.click(function(){
    $("html, body").animate({
        scrollTop:"-=" + scrollPxH + "px"
    });
});