Alternatives to background-attachment: fixed

后端 未结 2 906
眼角桃花
眼角桃花 2021-01-21 08:20

Since background-attachment:fixed doesn\'t work on some mobile browsers (e.g. on iOS), what alternatives are there, specifically when trying to implement the following demo: htt

2条回答
  •  轮回少年
    2021-01-21 09:20

    jQuery alternative to background-attachment:fixed; (be sure to have jQuery properly installed):

    $(window).scroll(function() {
         var scrolledY = $(window).scrollTop();
         $('.sec1').css('background-position', 'center ' + ((scrolledY)) + 'px');
    });
    

    It is important to note that your background image must not be taller than the height of the div or things get thrown off.

提交回复
热议问题