javascript - 聚焦内部 div 时滚动 div

标签 javascript jquery html css

我需要制作一个可滚动的 div,即使鼠标在内容上(在可滚动的 div 内)也可以滚动,而不仅仅是在它旁边(它是空白的)。这是我目前所拥有的:

var main = document.getElementById('main-site');
var maxTop = main.parentNode.scrollHeight-main.offsetHeight;

main.parentNode.parentNode.onscroll = function() {
   main.style.top = Math.min(this.scrollTop,maxTop) + "px";
}

在 Chrome 中是可以的 在 IE8+ 中没问题(我知道 hack) 在 Safari 中,当我滚动时内容抖动很多,我可以解决这个问题吗? (我想解决这个问题)

工作 fiddle -> https://jsfiddle.net/8oj0sge4/6/

var main = document.getElementById('main-site');
var maxTop = main.parentNode.scrollHeight - main.offsetHeight;

main.parentNode.parentNode.onscroll = function() {
  main.style.top = Math.min(this.scrollTop, maxTop) + "px";
}
#wrapper {
  width: 100%;
  height: 1500px;
  border: 1px solid red;
  padding-top: 380px;
}
#wrapper .container {
  border: 1px solid green;
  width: 100%;
  height: 500px;
  overflow: scroll;
}
#wrapper .container-scroll {
  height: 1500px;
  width: 100%;
  border: 1px solid yellow;
  position: relative;
}
#wrapper .main {
  width: 200px;
  height: 500px;
  background: black;
  overflow: scroll;
  position: absolute;
  color: white;
  left: 50%;
  margin-left: -100px;
  margin-top: 10px;
}
<div id="wrapper">
  <div class="container">
    <div class="container-scroll">
      <div id="main-site" class="main">
        My goals is to make the div container scroll also when the mouse is hover this div in safari, in Google and IE8 i already know how to make work, but safari is shaking a lot!
      </div>
    </div>
  </div>
</div>

谢谢你们。

最佳答案

我希望这个演示可以帮助您在鼠标悬停和鼠标离开 div 时使 div 内容滚动。

<html>
</head>
<style>
.mydiv
{height: 50px;width: 100px; overflow-y: scroll; }
</style>

<script>
function loadpage()
{ document.getElementById('marquee1').stop();  }
function marqueenow()
{ document.getElementById('marquee1').start(); }
</script>

</head>
<body onload="loadpage()">
<marquee id="marquee1" class="mydiv" onmouseover="marqueenow()"     onmouseout="loadpage()" behavior="scroll" direction="up" scrollamount="10">
This is my test content This is my test content This is my test content This     is my test content This is my test content This is my test content This is my     test 

content This is my test content This is my test content This is my test     content This is my test content This is my test content This is my test content     This is my test content This is my test content This is my test content 
</marquee>
</body>
</html>

关于javascript - 聚焦内部 div 时滚动 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29686645/

相关文章:

jquery - 提交翻转按钮在 FF 和 IE 中不起作用

html - 如何在 CSS 和 HTML 中打断长单词

javascript - 双击时是否有网格单元格(粘贴)事件

javascript - AngularJS "{{}}"站内标题属性不起作用

javascript - 使用 jszip 将 pdf 的 zip 保存到文件夹中

javascript - 为什么导航菜单被切断了?我们希望空白填充在菜单项后面

html - 控制渲染流程图

javascript - 调用函数而无需每次都重新初始化

javascript - 使用 THREEJS,我试图在场景中切换雾

jquery - 在mvc中实现部分 View 分页的最佳方式