根据精确的滚动百分比更改文本
Change text based on exact scroll percentage?
我正试图根据用户滚动内容的确切百分比来循环浏览编号的问题。我的目标是让这项工作负责任。
我已经创建了一个样品小提琴,这样你就可以看到我正在尝试做什么…
$(document).ready(
$(window).scroll(function(){
var progress = $(this).scrollTop() / $(document).height();
//calculate the percentage of the total window that the user has scrolled
var questNum = progress * 4;
//multiply that by the total number of questions, to get the corresponding question number
if (questNum < 1) {
$('#question').text('Hello?');
}
else if (questNum < 2) {
$('#question').text("It's me...");
}
else if (questNum < 3) {
$('#question').text('I was wondering if after all these years...');
}
else if (questNum < 4) {
$('#question').text('You'd like to meet.');
}
else{
$('#question').text('*ring ring*');
};
});
);
*{
height: 500px;
font-family: sans-serif;
font-size: 1em;
font-weight: 100;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<span id="question" style="position: fixed">...</span>
</div>
理论上,它似乎应该起作用,但我太迷茫了。谢谢
一些需要更正或更改的内容:
$(document).ready
需要一个函数作为其参数,所以添加function() { ... }
- 当单引号出现在单引号字符串中时,需要使用反斜杠进行转义:
'It''s me...'
和'You''d like to meet.'
-
由于您不知道代码将在哪个窗口大小上运行,将
500px
设置为窗口高度只会在某些设备上产生所需的效果。相反,动态设置高度:$('body').height($(document).height() + slackSpace);
其中slackSpace应该是您希望可用于滚动的像素数。
-
当你有更多的问题时,你留给滚动的空间(slackSpace)应该更大,所以它应该取决于问题的数量。在切换到下一个问题之前,您需要决定需要滚动多少像素。因此,您可以定义slackSpace如下:
var slackSpace = 5 * scrollPerQuestion;
其中CCD_ 6将是问题的数量。这个数字应该更好地动态管理(参见下一点)。
-
如果你把问题放在一个数组中,管理起来会更容易。然后你就不需要这些
if .. else if ... else if ...
了,只需要根据问题文本的编号来选择:var questions = [ 'Hello?', 'It''s me...', 'I was wondering if after all these years...', 'You''d like to meet.', '*ring ring*' ]; // ... once you have the questNum value: $('#question').text(questions[questNum]);
通过这种方式,您还可以访问问题数量:
questions.length
。 -
进度的计算不正确。这两个部分衡量的是不同的方面:滚动顶部给出(可见)内容的顶部,而文档高度对应于所有内容的底部偏移量。两者之间的区别至少是窗户的高度,所以你永远不会有100%的进步。这甚至可能使一些问题变得遥不可及。相反,使用这个:
var progress = $(window).scrollTop() / slackSpace;
其中slackSpace是上一点中定义的值。
-
有些浏览器在您返回页面或刷新页面时会保留以前的滚动位置。因此,每当加载页面时,您都希望将页面滚动到顶部。
-
文档的每一侧都有一些像素的默认边距。这严重影响了进度的计算。为了简化操作,请将该边距设置为零,并对问题元素本身应用一些间距,以便文本仍然显示在距离窗口边界很近的位置(也可以为问题元素指定类名而不是内联样式):
body { margin: 0px; } .question { position: fixed; padding: 10px; }
以下是应用所有这些想法的代码:
$(document).ready(function() {
var questions = [
'Hello?',
'It''s me...',
'I was wondering if after all these years...',
'You''d like to meet.',
'*ring ring*'
];
// How many pixels to scroll before going to next question
var scrollPerQuestion = 50;
// Total space needed to scroll through all questions
var slackSpace = questions.length * scrollPerQuestion;
// Set appropriate document height for scrolling all questions:
$('body').height($(document).height() + slackSpace);
$(window).scroll(function(){
// Calculate the percentage of the total window that the user has scrolled
var progress = $(window).scrollTop() / slackSpace;
// Convert progress into question number
var questNum = Math.floor(progress * questions.length);
// Make sure the question number does not pass the maximum
var questNum = Math.min(questNum, questions.length);
// Display corresponding question
$('#question').text(questions[questNum]);
});
// Scroll to top on page load
$(window).scrollTop(0).trigger('scroll');
});
body {
font-family: sans-serif;
font-size: 1em;
font-weight: 100;
margin: 0px;
}
.question {
position: fixed;
padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<span id="question" class="question">...</span>
</div>
它将完成片段中的工作。
我已经在上修改了您的代码
$(document).ready(function){
和
$('#question').text('您想见一面。');
试试这个。它应该工作:
$(document).ready(function(){ //$(document).ready(
$(window).scroll(function(){
var progress = $(this).scrollTop() / $(document).height();
//calculate the percentage of the total window that the user has scrolled
var questNum = progress * 4;
//multiply that by the total number of questions, to get the corresponding question number
if (questNum < 1) {
$('#question').text('Hello?');
}
else if (questNum < 2) {
$('#question').text("It's me...");
}
else if (questNum < 3) {
$('#question').text('I was wondering if after all these years...');
}
else if (questNum < 4) {
$('#question').text('You''d like to meet.');// $('#question').text('You'd like to meet.');
}
else{
$('#question').text('*ring ring*');
}
});
});//);
- 如何使用jquery在填充自动完成的值后使文本框只读
- 使用Clipboard.js复制span文本
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 用程序搜索JQuery数据表中的文本
- jQuery匹配JSON对象的部分文本
- onkeyup无法动态创建多个文本区域
- 如何在下面的ES6循环中获得前面的文本
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 让文本输入幻灯片显示输入时的新文本输入?然后向后滑动
- 借助asp.net验证或java脚本对多个文本进行验证
- 无法在ajaxStart中更改跨度文本
- 显示php中的加载百分比文本
- 根据精确的滚动百分比更改文本
- 基于编辑距离的javascript文本相似度百分比算法
- 循环进度条在滚动时显示 - 百分比文本问题
- 使用 HTML 文本框值作为高图表百分比
- 如何使元素中的文本仅在基于百分比而非像素查看页面的特定部分时显示
- 如何只允许文本框只包含数字和下面的百分比符号?
- 文本区域中更改值的百分比
- 如何将表单中的文本输入字段的宽度设置为页面宽度的百分比