简体   繁体   English

当光标在右列上方时如何滚动左列?

[英]How to scroll left column when cursor is above right column?

How to scroll up and down the left column when I'm scrolling over the map canvas? 当我滚动地图画布时,如何向上和向下滚动左列?

Map has disabled scroll wheel propagation, it would be nice to accomplish it if possible only by CSS. Map已禁用滚轮传播,如果可能,只有CSS才能完成它。 I've tried to wrap #map_canvas by other div with flex property and set map to position absolute and 100vh/vw, but it doesn't make difference with wheel bubble. 我试图用其他div用flex属性包装#map_canvas并将map设置为绝对位置和100vh / vw,但它与轮泡没有区别。

 $(document).ready(function() { var post = "<h3>Post title</h3><div>Donec id elit non mi porta gravida at eget metus. Maecenas sed diam eget risus varius blandit.</div><hr>"; var i = 0; while (i < 10) { $('#left_container').append(post); i++; } var options = { zoom: 10, scrollwheel: false, center: new google.maps.LatLng(49, 17) }; var map = new google.maps.Map($('#map_canvas')[0], options); $("#map_canvas").scrollLeft(); }); 
 .flexbox-container { display: -ms-flex; display: -webkit-flex; display: flex; flex-direction: row; flex-wrap: wrap; height: 100vh; } .flexbox-container #left_container { flex: 1; padding: 0 5px; overflow-y: auto; height: 100vh; } #map_canvas { flex: 2; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?v=3&amp;sensor=false"></script> <div class="flexbox-container"> <div id="left_container"></div> <div id="map_canvas"></div> </div> 

jsfiddle 的jsfiddle

In this solution, I am assuming map is 200px wide. 在这个解决方案中,我假设map是200px宽。 You can replace it by another value in css and js. 您可以用css和js中的另一个值替换它。

You want to scroll the leftmost column when the mouse is hover the rightmost (map) column. 当鼠标悬停在最右边(地图)列时,您希望滚动最左侧的列。
To accomplish this, the leftmost column should actually occupy all the viewport, although visually does not look like that. 要实现这一点,最左边的列应该实际占用所有视口,尽管视觉上看起来不像那样。

In the #left_container below, you can see that is absolutely placed to fit all .flexbox-container parent boundaries. 在下面的#left_container中,您可以看到绝对适合所有.flexbox-container父边界。 The trick part is the border-right: 200px transparent solid that "pushes" the vertical scroll bar to the left. 技巧部分是border-right: 200px transparent solid ,将“垂直滚动条”“推”到左侧。

So, when you move your mouse wheel on the map, you are actually hovering the #left_container 's right border and thus moving it up and down, not the map. 因此,当您在地图上移动鼠标滚轮时,您实际上正在徘徊#left_container的右边框,从而上下移动它,而不是地图。

.flexbox-container {
  height: 90vh;
  position: relative;
}

#left_container {
  padding: 0 5px;
  overflow-y: auto;
  display: inline-block;
  height: 100%;
  border-right: 200px transparent solid;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 1;
  pointer-events: none;
}

#left_container.scrollMap {
  pointer-events: auto;
}

#map_canvas {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: 200px;
}

The above CSS solution, completely ignores other maps events (dblclick, click,...) 上面的CSS解决方案,完全忽略了其他map事件(dblclick,click,...)
To fix that, we need a bit of JS to ignore mouse events on the leftmost column (see pointer-events ): 要解决这个问题,我们需要一些JS来忽略最左边一列的鼠标事件(参见指针事件 ):

  var map_canvas = $('#map_canvas')[0],
      $left_container = $('#left_container'),
      $parent = $('.flexbox-container'),
      options = {
          zoom: 10,
          scrollwheel: false,
          center: new google.maps.LatLng(49, 17)
      },
      map = new google.maps.Map(map_canvas, options),
      wheelEvent = function (e) {
          $left_container.addClass('scrollMap');
      };

  map_canvas.addEventListener('mousewheel', wheelEvent);
  map_canvas.addEventListener('DOMMouseScroll', wheelEvent);
  $parent.mousemove(function (e) {
    if (e.clientX < $parent.width() - 200) {
      $left_container.addClass('scrollMap');
    } else {
      $left_container.removeClass('scrollMap');
    }    
  });


<div class="flexbox-container">
    <div id="left_container" class="scrollMap"></div>
    <div id="map_canvas"></div>
</div>

Here's a way to transfer the scroll event of a container #canvas_container to the left column by javascript. 这是一种通过javascript将容器#canvas_container的滚动事件传输到左列的方法。 To accomplish this, the map has to be inside a container and padded at the top and bottom ( .padder elements). 要实现此目的,地图必须位于容器内并填充在顶部和底部( .padder元素)。

This way you can theoretically scroll up and down, catch this scroll event and transfer it to the left column. 这样你理论上可以向上和向下滚动,捕捉这个滚动事件并将其传递到左列。 Other events are not touched. 其他事件没有触及。

The key part is this javascript: 关键部分是这个javascript:

  $canvas_container = $('#canvas_container')
  $left_container = $('#left_container');
  initial_offset = 100;

  $canvas_container.scrollTop(initial_offset);

  $canvas_container.on('scroll', function(e) {
    $left_container.scrollTop($left_container.scrollTop() + $canvas_container.scrollTop() - initial_offset);
    $canvas_container.scrollTop(initial_offset);
  });

Only drawback I see atm is the visible vertical scroll bar on the map. 我看到atm的唯一缺点是地图上可见的垂直滚动条。

To play around by yourself, see this fiddle . 要自己玩,看看这个小提琴

 $(document).ready(function() { var post = "<h3>Post title</h3><div>Donec id elit non mi porta gravida at eget metus. Maecenas sed diam eget risus varius blandit.</div><hr>"; var i = 0; while (i < 10) { $('#left_container').append(post); i++; } var options = { zoom: 10, scrollwheel: false, center: new google.maps.LatLng(49, 17) }; var map = new google.maps.Map($('#map_canvas')[0], options); $canvas_container = $('#canvas_container') $left_container = $('#left_container'); initial_offset = 100; // same as height of .padder $canvas_container.scrollTop(initial_offset); $canvas_container.on('scroll', function(e) { $left_container.scrollTop($left_container.scrollTop() + $canvas_container.scrollTop() - initial_offset); $canvas_container.scrollTop(initial_offset); }); }); 
 .flexbox-container { display: -ms-flex; display: -webkit-flex; display: flex; flex-direction: row; flex-wrap: wrap; height: 80vh; } .flexbox-container #left_container { flex: 1; padding: 0 5px; overflow-y: auto; height: 80vh; } #canvas_container { flex: 2; height: 80vh; overflow-y: scroll; } #map_canvas { height: 100%; } .padder { height: 100px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?v=3&amp;sensor=false"></script> <div class="flexbox-container"> <div id="left_container"></div> <div id="canvas_container"> <div class="padder"></div> <div id="map_canvas"></div> <div class="padder"></div> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM