Issue with SharePoint back-to-top functionality: unable to apply custom class during page scrolling

I am currently working on implementing a "back to top" functionality in the SharePoint master page. The back to top feature itself is functional, however, I am encountering an issue with adding a class that will show or hide the icon based on page scroll. Below is the JavaScript code that I am using:

if ($('#back-to-top').length) {
    var scrollTrigger = 100; // pixels
    var backToTop = function () {
        var scrollTop = $(window).scrollTop();
        if (scrollTop > scrollTrigger) {
            $('#back-to-top').addClass('show');
        } else {
            $('#back-to-top').removeClass('show');
        }
    };

    $(window).on('scroll', function () {
        backToTop();
    });

    $('#back-to-top').on('click', function (e) {
        e.preventDefault();
        $('#s4-workspace').animate({scrollTop: 0}, 700);
    });
}

The following HTML code snippet has been added to the SharePoint master-page:

<a href="#" id="back-to-top" title="Back to top">&uarr;</a>

Current Issue: The icon is not visible on the page because the class 'show' is not being added during scrolling.

Answer №1

Instead of using $(window) for SharePoint, I have successfully implemented $('#s4-workspace').

if ($('#back-to-top').length) {
var scrollTrigger = 100, // px
backToTop = function () {
var scrollTop = $('#s4-workspace').scrollTop();
if (scrollTop > scrollTrigger) {
$('#back-to-top').addClass('show');
} else {
$('#back-to-top').removeClass('show');
}
};

$('#s4-workspace').bind('scroll', function () {
backToTop();
});

$('#back-to-top').on('click', function (e) {
e.preventDefault();
$('#s4-workspace').animate({scrollTop: 0}, 700);
});
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Can someone guide me on how to display only the most recent form submission in a form using React and JavaScript? Appreciate your help

Currently, I'm facing a challenge with displaying the latest form submission below a form on my page. Instead of showing all form submissions, I only want to display the most recent one. I'm seeking advice on how best to tackle this issue. If it ...

Using jQuery to call a function within another function

Here is some code that I am working with: function open_viewer() { $('.image_thumb').click(function(){ var reqz = 'id='+$(this).attr('id')+'&i='+$(this).attr('i'); $.ajax({ type: ' ...

Choosing the Date Picker in Selenium WebDriver: A Step-by-Step Guide

Currently delving into Selenium WebDriver with a focus on Java. My goal is to navigate through a date picker dropdown and select specific values for date, month, and year. Here is an example of the HTML tag: <dd id="date-element"> <input id="fro ...

Dragging a value from a chip in Vuetify and dropping it into an input does not function as expected

I am facing an issue with implementing drag-and-drop functionality using v-chip elements and a form. My goal is to enable users to drag any chip and drop it into the form so that the input field in the form captures the value of the dropped chip. Here is ...

Chrome successfully processes Ajax request, but Firefox encounters failure

I've encountered a peculiar issue with a JavaScript function that is responsible for making an Ajax call to a PHP page for database transactions and data processing. Take a look at the function below: function processQuizResults() { console.log(" ...

Ways to retrieve the data from promises after they have been resolved?

I'm struggling to retrieve the values from getPeople(0,4). function getPeople(start, end) { const peopleArray = []; for (let i = start; i <= end; i++) { peopleArray.push( axios.get(`https://www.testsite.net/api/test/workers/ ...

Error: VueJS mixins do not include the property definition

I've been trying to incorporate Mixins into my Vue.js code, but I've run into a few issues :/ Here's the current code for two test modules : ErrorBaseMixin.vue <script> import ErrorAlert from './ErrorAlert'; expor ...

Invoke the URL with a query string and obtain the response from the specified web page

How can I make a button on my user.php page call action.php?name=john&id=10 without refreshing the page? The action.php page needs to process the query string data (name=john & id=10), execute some action, and then send a message back. Here is an ...

What is the best way to extract keys from a hash in Ruby using a Javascript string?

I am currently developing a command line tool using Ruby that is designed to parse JSON data from diverse sources and perform certain operations on the retrieved information. To make it user-friendly, I have incorporated a feature where users can configure ...

Display with organized information, Arrange with unprocessed data in DataTables.net

Below is a snippet of the datatables configuration I am working with: { "dom" : "rltip", "processing" : true, "serverSide" : false, "order" : [ [ 1 , "desc" ] ], "searching" : false, data: [ { "column-a" : "Samp ...

What are some strategies for implementing dynamic script generation within an AJAX response?

I am exploring a new AJAX design approach where a script is returned to handle and show data. The JSON response below is currently functional, but I would appreciate advice on how to better organize the application for future maintenance. { payload: " ...

Unable to send HTML content back from the controller when making Ajax requests in a messaging application

Currently, I am in the process of developing an integrated chat application within CRM. The main functionality involves displaying the chat history between users when a logged-in user clicks on a contact using Ajax calls. However, I have encountered some i ...

Is it necessary to implement a restful API for all database interactions in my Node.js application?

Currently, I am in the process of developing a simple blogging platform utilizing mongoose, node, express, Jade, and bootstrap. As I tackle the task of loading post content, I find myself contemplating whether to conduct database operations directly within ...

How can we enhance the integration of HTML and PHP for a smoother workflow?

Are there alternative solutions, aside from using a template engine like Smarty, for managing large code like this? <table class="table table-striped dataTable table-bordered"> <thead> <tr> <?php $o ...

Highlighting menu elements when landing on a page and making another menu element bold upon clicking in WordPress

I've been searching extensively for a solution to this issue. I'm trying to make the menu item DE appear bold when entering the site, and if I click on EN, I want DE to return to normal (thin) font while EN becomes bold. You can find the site he ...

What methods can be used to validate an undefined value?

exploding right now if (myObject.myKey.myThing){ } undefined myThing cannot be read What approach can be taken to determine if myThing is defined before entering the if block? ...

Display a hidden div using jQuery Validate and ajaxSubmit

Currently, I am utilizing jQuery Validate in combination with ajaxSubmit to send a multipart/form-data form to my CI controller. //Form Submission Functions $('#newEventSubmit').click(function() { $("#newEventForm").validate({ ...

Can IE Conditional Comments have a negative impact on page speed?

Debates between "CSS hacks" and "Conditional Comments" have become a regular occurrence for me lately, so I wanted to hear the thoughts of the Stack Overflow community on this topic. <!--[if IE]> <link type="text/css" rel="stylesheet" href="ie-s ...

Verify that the computer is connected to the Internet by sending an ajax request to Google

Whenever I need to test my internet connection, I rely on the following code snippet: const checkInternetConnection = () => { $('input').ajaxError(function(){ alert("failed"); }); $.get('http://www.google.com', f ...

Maintaining photo proportions in HTML when using width and height as percentages

Is it possible to specify width and height as percentages in the <img> attributes? It seems that when I attempt to do so, the image resizes but the quality appears skewed. Here is an example of my markup with fixed attributes: <img src="#" widt ...