私は、ユーザーがスクロールした内容の正確なパーセンテージに基づいて、番号付きの質問を繰り返すことを試みています。私の目標は、これを反応的に働かせることです。正確なスクロールパーセンテージに基づいてテキストを変更しますか?
あなたは私がやろうとしているものを見るように、私はそれが動作するはずのようにそれはそう...理論的には
$(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) {
\t $('#question').text('Hello?');
}
else if (questNum < 2) {
\t $('#question').text("It's me...");
}
else if (questNum < 3) {
\t $('#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>
をサンプルフィドルを作成しましたが、私は」私は失われた。ありがとう!
私はまだJS/jQueryのnoob段階に入っています。あなたはコードを修正するだけでなく、ロジックに欠陥があることをどれだけ評価するか分かりません。私は実際にそれをコピーし、それがなぜ機能するのか理解していないのに対し、このコードからより良い習慣を学ぶようになる。ありがとうございました!!! – tiffanyaych
ようこそ。 – trincot