magento2.3 – How to remove back to top in magento 2?


Ok So I’ve checked the website and I could see this element in dev tools.
When scrolling page I saw JS sets inline style for it so I set breakpoint on this element

Setting a breakpoint

so when scrolling it stopped in one of Jquery method. Then using call stack panel I found the source:

Script responsible for adding a go to top element

So you need to override themecore.min.js

As far as I’ve checked it’s not easy to add mixin for this js script. So you need to replace this file with version and remove backToTop method call with method itself.

themecore.init = function() {
    var self = this;
    this.triggerAdaptScreen();
    this.triggerAdaptScreen(1200);

    ... // some code - I've cut it to make example short

    this.remoteSliderNav();
    this.backToTop(); // remove this line
    this.scrollTo();
    this.mobiProductViewTabs();

and then you can remove:

themecore.stickyMenu = function() {
    $('.js-sticky-menu').attr('data-mage-init', '{"themewidgets": {"codazon.stickyMenu": {}}}');
}
// remove this part below:
themecore.backToTop = function() {
    if ($('#back-top').length == 0) {
        $('<div id="back-top" class="back-top" data-role="back_top"><a title="Top" href="#top">Top</a></div>').appendTo('body');
    }
    $('(data-role="back_top")').each(function() {
        var $bt = $(this);
        $bt.click(function(e) {
            e.preventDefault();
            $('html, body').animate({
                'scrollTop': 0
            }, 800);
        });
        function toggleButton(hide) {
            if (hide) {
                $bt.fadeOut(300);
            } else {
                $bt.fadeIn(300);
            }
        }
        var hide = ($(window).scrollTop() < 100);
        toggleButton(hide);
        $(window).scroll(function() {
            var newState = ($(window).scrollTop() < 100);
            if (newState != hide) {
                hide = newState;
                toggleButton(hide);
            }
        });
    });
}
// end of removing
themecore.b64DecodeUnicode = function(str) {
    return decodeURIComponent(Array.prototype.map.call(atob(str), function(c) {
        return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
    }).join(''));
}

Other option would be to run JS after this script init module and remove this element but I would prefer to cut down amount of JS code if not used.

Idealy if author of this themecore script would split it into smaller files so you could override/mixin only part of the code so updates will be easier.

Overriding whole file you need to remember about future updates of this file.

How to replace this file?
You need find where/how themecore is initalized.

Then I guess this docs will help you:
https://devdocs.magento.com/guides/v2.4/javascript-dev-guide/javascript/custom_js.html