0

I need the javascript to load after the user scrolls. Right now I have a .phtml file that just has this,

<div id="razoyo-gtm-user-code" style="display:none;" data-mage-init='{ "razoyoGlobalUserCode": { "pageType": "<?= $block->escapeJs($block->getPageType()); ?>"} }'> 

Which loads razoyoGlobalUserCode as soon as the page is done loading. But I need the file to load after the user scrolls. So I've tried this:

<script>  
    var razoyo = document.createElement('div');
    razoyo.id = 'razoyo-gtm-user-code';
    razoyo.style = 'display:none';    
    razoyo.setAttribute("data-mage-init", '{ "razoyoTest": { "pageType": "<?= $block->escapeJs($block->getPageType()); ?>"} }');

    document.addEventListener('scroll', function(e) {   
    document.head.appendChild(razoyo);
    });
</script>

However this doesnt fix the issue at all. If someone has any advice on what I can do I would appreciate it a lot. Thanks.

1 Answer 1

0

Try This:

var secret = false,
        testTop = $('#test').offset().top,
    testHeight = $('#test').height(),
    scrlTop;

$(window).on('scroll', function(){
    scrlTop = $(this).scrollTop();
  if(scrlTop > testTop && scrlTop < testTop+testHeight){
    if(!secret){
        console.log('call me once please');
      secret = true;
    }   
  }else{
    secret = false;     
  }
});
1
  • replace your code with this one... Commented Apr 26, 2021 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.