- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作下面的演示,并希望将带有百分比值(例如 50%
)的scrollTop 应用于scrollTop。这可能吗?
$("button").on("click", function(){
$('#sc').animate({ scrollTop: 50+"%" }, 1);
});
#sc {
background: khaki;
width: 400px;
height: 200px;
position: relative;
margin:20px;
padding:0px;
overflow:hidden;
overflow-y: auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>Scrool 50%</button>
<div id="sc">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
</div>
最佳答案
如果您想要滚动到总可滚动距离的中点,那么您需要自己手动计算像素值。元素的可滚动高度就是其 clientHeight
之间的差异。和 scrollHeight
。这些属性在 HTML 元素本身上可用,因此我们需要首先使用 $('#sc')[0]
获取它,它返回 DOM 节点:
$("button").on("click", function(){
var sc = $('#sc')[0];
var scrollableHeight = sc.scrollHeight - sc.clientHeight;
$('#sc').animate({ scrollTop: 0.5 * scrollableHeight }, 1);
});
查看概念验证示例:
$("button").on("click", function(){
var sc = $('#sc')[0];
var scrollableHeight = sc.scrollHeight - sc.clientHeight;
$('#sc').animate({ scrollTop: 0.5 * scrollableHeight }, 1);
});
#sc {
background: khaki;
width: 400px;
height: 200px;
position: relative;
margin:20px;
padding:0px;
overflow:hidden;
overflow-y: auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>Scrool 50%</button>
<div id="sc">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
</div>
关于javascript - 在scrollTop上将div设置为百分比值的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60702079/
我收到错误: body.scrollTop 在严格模式下已弃用。请在严格模式下使用“documentElement.scrollTop”,在怪异模式下使用“body.scrollTop”。 我的代码是
两者有什么区别: $(window).scrollTop() 和 $(document).scrollTop() 谢谢。 最佳答案 它们都有相同的效果。 但是,正如评论中所指出的: $(window)
我不太明白这一点,因为有时 body,html 有效,有时 window.我已经用 jQuery 完成了 2 个网页(本地);在第一个主体中,html 可以工作,而在另一个主体中则不行,所以我编写了
我遇到了一个仅在 Safari 中发生的问题。我有一个特定的 div,我试图在页面加载后自动滚动到特定位置。 如果我在 Safari 中输入页面的 URL 并按 Enter 键,则 div 无法正常滚
我有这段代码,我想在 div 而不是正文上滚动 var scrollPosition = document.body.scrollTop || document.documentElement.scr
我是 jQuery 的新手,所以请不要介意我。 我有这个风格 html, body{ height: 100%; overflow: auto; } 还有这个脚本 $(document
我只想获取文档的滚动条并使用此 answer 中的代码.但我得到的是一个“窗口”对象。我已经用 IE 10、Chrome 和 Firefox 进行了测试。这是我的代码: var doc = d
当试图找出网页从顶部滚动了多少时,应该使用以下哪一个: document.body.scrollTop, document.documentElement.scrollTop, window.page
我有一个导航,初始网址如下所示: test.php?query=19 我的页面上有像这样的链接 Section 1Section 2Section 3 分为 3 个部分: 并且我正在使用此 jque
如何在没有动画的情况下使用滚动顶部 此代码有效: var offTop = $('#box').offset().top; offTop = offTop-43; $('#mainCt').anim
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
当我使用函数scrollTop 获取顶部的像素数时,我收到的像素数为1、4、20...,具体取决于我滚动的速度。 $(window).scroll(function(){ var positi
嘿,我试图通过向左移动 10% 和向右移动 10% 来让 2 个 div 相互移开。在 1000 点之后它们会相互移开,但在我向后滚动到 1000 点之前它们不会再移回来 $(document).re
我有一些带有 .class 类的 div,其滚动位置由其父级的另一个子级定义,因此我需要分配一个 scrollTop () 与 $(this),类似 $(".class").scrollTop($(t
我在使用 ScrollTop 效果时遇到一些困难。事实上,它不起作用,我也不明白为什么。 这是我的代码的 Javascript 部分: $('.hcb_link').click(function(){
我想将自定义滚动写入部分功能。 当我加载页面并向下滚动时,它应该向下滚动窗口的高度,这很好。 然后,如果再次向下滚动,我想检查页面是否滚动超过窗口高度(如果向下滚动,则应该是),但它似乎卡在第一次滚动
我得到了下面的函数,该函数会弹出一个窗口,但无法将主页滚动到适当的 div 高度。 function open_window(getDiv) { var objDiv = document.getEl
我编写了这段代码来计算到页面顶部的距离。根据距离是否为 0,它会使图像消失/出现,并向上/向下移动辅助图像。 $(window).scroll(function(){ if ($(this).
我正在努力让scrolltop()返回一个值。现在它只给出 0。我知道这是一个简单的问题,但有人有任何建议吗? 这是 JQuery: $(document).ready(function(){ $('
我在此菜单上使用 jQuery 滚动顶部功能:http://goethesternfriseure.de/index.php 问题是滚动功能仅在第一次起作用。第二次点击链接后,它会滚动到底部。 $('
我是一名优秀的程序员,十分优秀!