前提・実現したいこと
スクロールに応じて出現と画面外に引っ込む動作をスクロール毎に繰り返すイベントを行いたい。
参考サイト
該当のソースコード
<html> <style> .floating { display: table; height: 300px; width: 300px; border-radius: 50%; background-color: #d10000; position: fixed; bottom: 5rem; right: calc(-300px + -5rem); transform: rotate(0); transition: 1.5s cubic-bezier(0.19, 1, 0.22, 1); } .floating > div { display: table-cell; vertical-align: middle; text-align: center; } .floating > div > span { color: #f1f1f1; font-weight: 600; } .floating > div > span:first-child { display: block; font-size: 3rem; margin-bottom: 1rem; } .floating > div > span:nth-child(2) { padding: 10px 20px; border: 4px solid; border-radius: 30px; } </style> <body> <div id="section_wrapper"> <section></section> <section></section> <section></section> <section></section> <section></section> </div> <script> $(window).on('load' , function() { $('#section-wrapper').append('<div class="floating"><div><span></span><span></span>'); $('.floating > div > span:first-child').text('03-1234-5678'); $('.floating > div > span:nth-child(2)').text('お問い合わせはコチラ!'); }); $(window).on('scroll' , function() { var hideHeight = $('section:nth-last-of-type(2)').offset().top; if ($(this).scrollTop() > 500 && $(this).scrollTop() < hideHeight ) { $('.floating').css({ 'right' : '5rem' , 'transform' : 'rotate(720deg)' }); } else { $('.floating').css({ 'right' : 'calc(-300px + -5rem)' , 'transform' : 'rotate(0)' }); } }); </script> </body> </html>
試したこと
そこで上記に加えてスクロールイベント中は初期位置に戻り、
スクロールが一定時間止まったら、500以上hideHight以下にあるかぎりcssの変更をさせようと思い、
こちらのサイトを参考に新しくスクロールストップのイベントをつくり、
スクロールストップをしたら上記動作を実行させ、スクロールイベントが発生時に.floating
がright: 5rem;
もしくはtransform: rotate(720deg)
であれば初期位置にもどすような下記コードを書き足したのですが、エラーは出ないものの動作はしませんでした。
(一度出現したら初期位置に戻りませんでした;)
$(window).on('scroll' , function() { var right = $('.floating').css('right'); var transform = $('.floating').css('transforme'); if ( right == '5rem' || transform == 'rotate(720deg)' ) { $('.floating').css({ 'right' : 'calc(-300px + -5rem)' , 'transform' : 'rotate(0)' }); } else { // } });
補足情報(FW/ツールのバージョンなど)
参考サイトのようなスクロールイベントが発生中は初期位置にもどり、スクロールストップしたら指定位置にくるような動きが理想です。
稚拙な記述でお恥ずかしいのですが、何かヒントを教授頂けますと幸いです。
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー