【问题标题】:scrollLeft works but scrollTop doesn't workscrollLeft 有效,但 scrollTop 无效
【发布时间】:2012-11-26 10:08:47
【问题描述】:

我有以下带有 CSS .container { overflow: scroll; } 等的 HTML。

<body>
<div class="container">
  <div id="markers"></div>
  <img id="map" src="/img/map.jpg"/>
</div>
</body>

map.jpg 很大,我想滚动到这样的固定位置:

$(function(){
  console.log($('.container').scrollTop());
  $('.container').scrollTop(1000);
  console.log($('.container').scrollTop());
  console.log($('.container').scrollLeft());
  $('.container').scrollLeft(1750);
  console.log($('.container').scrollLeft());
});

scrollLeft 工作正常,但 scrollTop 不行。下面是控制台输出。

0
0
0
1750

找了半个小时还是不知道怎么解决……


更新: CSS 关于 .container #markers#map

.container { overflow: scroll; width: 100%; max-width: 100%; height: 100%; max-height: 100%; margin: 0; padding: 0; }
#map { width: 5000px; max-width: 5000px; height: 2907px; max-height: 2907px; cursor: crosshair; }
#markers { position: relative; top: 0; left: 0; width: 0; height: 0; margin: 0; padding: 0; }

【问题讨论】:

  • 你能给我们展示更多关于 .container、#markers 和 #map 的 CSS 代码吗?
  • @Sybio 我已经更新了问题。
  • 这很奇怪,也许可以尝试溢出:auto on .container 而不是滚动。
  • @Sybio 我设法通过添加 position: absolute; 来修复它到.container
  • 我在下面的小提琴链接上做了一个例子,你可以看到他们想要什么

标签: jquery scroll


【解决方案1】:

查看此代码,您也可以在此处查看实时示例http://jsfiddle.net/sarfarazdesigner/2uvRe/

这是html代码

<div class="demo">
    <h1>lalala</h1>
    <p>Hello</p>
</div>​

这是css代码

div.demo {
background:#CCCCCC none repeat scroll 0 0;
border:3px solid #666666;
margin:5px;
padding:5px;
position:relative;
width:200px;
height:100px;
overflow:auto;
}
p { margin:10px;padding:5px;border:2px solid #666;width:1000px;height:1000px; }​

这是javascript代码

$("div.demo").scrollTop(300);
    $("div.demo").scrollLeft(300);​

【讨论】:

    【解决方案2】:

    经过几次试验,我设法通过将position: absolute; 添加到.container 来修复它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-28
      • 2011-05-17
      • 2017-01-08
      • 2017-01-08
      • 2012-05-08
      • 2017-03-15
      • 2014-10-30
      相关资源
      最近更新 更多