Skip to content

Instantly share code, notes, and snippets.

@nyawach
Created July 23, 2017 03:00
Show Gist options
  • Save nyawach/f9bbb0fa2e7ed15ac43cecb11a319ea7 to your computer and use it in GitHub Desktop.
Save nyawach/f9bbb0fa2e7ed15ac43cecb11a319ea7 to your computer and use it in GitHub Desktop.
背景固定(RAF/onScroll両方)
function setupFixedBg() {
const $fixed = $('.js-fixed-bg');
const $wrapper = isSP() ? $(window) : $('.wrapper');
let _tmpTop = $wrapper.scrollTop();
(function _update() {
const top = $wrapper.scrollTop();
if(top !== _tmpTop) {
$fixed.css('transform', `translateY(${top}px)`);
}
_tmpTop = top;
requestAnimationFrame(_update);
})();
// $wrapper.on('scroll', () => {
// const top = $wrapper.scrollTop();
// if(top !== _tmpTop) {
// $fixed.css('transform', `translateY(${top}px)`);
// }
// _tmpTop = top;
// })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment