What could be causing my JavaScript code to malfunction, even though it appears to be coded correctly?

// JavaScript Document
"use strict";
$(window).scroll(function(){
if($(window).scroll() > 100){
$("#scrollTop").fadeIn();
}
});

$(window).scroll(function(){
if($(window).scroll() < 100){
$("#scrollTop").fadeOut();
}
});

$(document).ready(function(){
$('.fa').click(function(){
$('html, body').animate({scrollTop : 0},1000);
return false;
});
});
#scrollTop{
position: fixed;
bottom: 20px;
right: 20px;
cursor: pointer;
display: none;
  background-color:#0D0155;
}

#scrollTop:after{
font-family: fontAwesome;
content:"\f102";
}

.fa {
padding: 20px;
width: 60px;
  font-size: 15px!important;
  text-align: center;
  text-decoration: none;
  margin: 5px 2px;
transition: all 0.3s ease-in-out;
-webkit-transition: all 0.3s ease-in-out;
-moz-transition: all 0.3s ease-in-out;
-o-transition: all 0.3s ease-in-out;
-ms-transition: all 0.3s ease-in-out;
transition-delay: 0.3s;
-webkit-transition-delay:0.3s;
background-image: -webkit-linear-gradient(20deg,#0D0155,#3A83F3);
    background-image: -o-linear-gradient(20deg,#0D0155,#3A83F3);
    background-image: linear-gradient(20deg,#0D0155,#3A83F3);
 color: white;
margin-left: 10px;

}

.fa:hover {
    background-image: -webkit-linear-gradient(left, #0D0155, #0D0155);
    background-image: -o-linear-gradient(left, #0D0155, #0D0155);
    background-image: linear-gradient(to right, #0D0155, #0D0155);
text-decoration: none;
color: #FFF;
}
<div class="fa" id="scrollTop"></div>

"use strict"
$(window).scroll(function(){
"use strict";
if($(window).scrollTop() > 300){
    $('#scrollTop').fadeIn();
    }
});

$(window).scroll(function(){
"use strict";
if($(window).scrollTop() < 300){
    $('#scrollTop').fadeOut();
    }
});

$(document).ready(function(){
"use strict";
$('#scrollTop').click(function(){
    $('html, body').animate({scrollTop : 0},1000);
    return false;
 });
});

Right, I'm working on implementing a fade-in button at the bottom of the screen that appears once the user scrolls past a specific distance from the top.

This button should smoothly scroll back to the top of the webpage when clicked.

Previously, this functionality was working as expected, but now it has suddenly stopped functioning properly and I find this unacceptable.

Attempts I've made to troubleshoot the issue include:

  • Adding the "use strict" directive at the beginning of the document.

  • Including the "use strict" directive within each of the code functions themselves.

  • Removing the "use strict" directive from each of the code functions.

  • Eliminating the "use strict" directive at the start of the document.

  • Trying switching from double quotes to single quotes.

  • Reverting from single quotes back to double quotes.

  • Embedding the JS directly into the webpage instead of linking to an external JS file.

  • Removing the embedded JS from the webpage and reverting to an external link in the head section.

  • Removing the "type:text.javascript" attribute from the script link in the head.

  • Reinstating the "type:text.javascript" attribute in the script link.

  • Testing with the ".fa" class in the JS code.

  • Using the "scrollTop" ID attribute for testing purposes.

The anticipated outcome was for the JavaScript to execute correctly based on the code instructions, but unfortunately, nothing happens. The button does not fade in, and clicking does not trigger any action.

Frankly speaking, I am frustrated with coding deciding not to function when it previously worked perfectly fine.

So why isn't it cooperating now?

Answer №1

Check out this functional jsFiddle example: https://jsfiddle.net/6cpf07ed/

Your code is solid, but remember to utilize the scrollTop() jQuery function instead of scroll()

$(window).scroll(function(){
    if($(window).scrollTop() > 100){
        $("#scrollTop").fadeIn();
        }
    });

$(window).scroll(function(){
    if($(window).scrollTop() < 100){
        $("#scrollTop").fadeOut();
        }
    });

$(document).ready(function() {
    $('.fa').click(function(){
        $('html, body').animate({scrollTop : 0},1000);
        return false;
    });
});

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

Safari and iOS 15 to 14 have rendered the Javascript audio context unusable

Recently, I noticed that the IOS/Safari upgrade has caused some issues with the web audio API. Check out this simple code that used to work on IOS and Safari before the upgrade, but now it doesn't work anymore. Interestingly, it still works on Firefo ...

textarea element enclosed by a div tag

I'm looking to add two side-by-side boxes on my page for comments. One box will display previous comments and should be disabled, while the other allows current users to leave comments. I achieved this by using two separate divs positioned next to eac ...

How to Retrieve the Order Number of an Object in an Array using JavaScript ES6

Curious about JavaScript ES6 and needing assistance! I have a simple question - how can I determine the order number of an object in an array? [ { "pk": 23, "image": "http://localhost:8000/media/users/1/2_27.jpg"}, { "pk": 11, "image": "http://localho ...

Unexpected behavior with VueJS Select2 directive not triggering @change event

Recently, I implemented the Select2 directive for VueJS 1.0.15 by following the example provided on their official page. However, I am facing an issue where I am unable to capture the @change event. Here is the HTML code snippet: <select v-select="ite ...

Is it possible to overlay a three.js scene onto an HTML video element?

A project I am working on involves developing an AR app that can display markers for North, East, South, and West using a smartphone camera. The goal is to create a scene with a transparent background that can be overlayed on a video feed from the camera. ...

Can Ajax, jQuery, or JavaScript be utilized to create a dropdown menu that automatically navigates to a specific section on the webpage?

I have been exploring this issue here, but I am unable to find any relevant information. Imagine I have a list that starts with <h2> tags: <h2>ITEM 1</h2> CONTENT ______________________ <h2>ITEM 2</h2> CONTENT ____________ ...

When using Vue.js and Quasar to implement multiple filtering forms, an issue arises where the input value disappears

I am currently exploring how to implement multi-item filtering in Quasar, a framework of Vue. At the moment, I am utilizing input and checkbox elements. Everything seems to be functioning correctly, but there is an issue with the select item disappearing. ...

Background images flanking both sides

What is the solution to making this function properly? <html> <head> <style type="text/css"> .left { float: left; background-image: url('image_with_variable_width.png'); background-repeat: repeat-y; } .right { float: right; bac ...

Sending an Ajax request using a dropdown menu

I'm having trouble retrieving a value from my database when a select option is chosen. The select options are generated dynamically from the same database. As a beginner in AJAX requests, I am struggling to figure out why I am only getting a blank re ...

Error loading advertising box within the list item due to nan issue

After adding an advertising image, my code seems to show a NaN error. Can someone please help me identify the source of this error and suggest how I can fix it? Here is a snippet of my code in HTML: <div> <ul class="list-groupJournalList&qu ...

Conceal zoom-in function in video element for Edge and Internet Explorer

I'm trying to get rid of the zoom in control on the video tag that only shows up in Edge and Internet Explorer. I've attached a snapshot for reference. I've searched for a solution but haven't had any luck. https://i.sstatic.net/7Lx8k. ...

In Meteor JS, MongoDB does not support using $addToSet on non-array values

I am looking to create an array of users who have upvoted a post, and only allow them to upvote once per post. I want to store the users who have upvoted in the array to prevent multiple clicks. However, when I use the $addToSet method on the array, MongoD ...

Choose carefully when to utilize forkJoin

In a particular scenario, I need an application to generate menus based on specific contexts. Here are the definitions for menuA and menuB: // menuA example from a given source menuA() { return [ { a: 'demo1', b: &apo ...

Define unique positions for multiple shapes and then combine these shapes

import * as THREE from 'three'; import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js'; const renderer = new THREE.WebGLRenderer(); renderer.setSize( window.innerWidth, window.innerHeight ); document.body.appe ...

How can one obtain the alphabet letters of a specific language?

Is there a way to programmatically retrieve the alphabet of a language (using its 2-letter ISO code) from an open-source repository? For instance: console.log(getAlphabet('en')); would result in: a b c d ... while console.log(getAlphabet(&apos ...

Develop a custom autocomplete feature using Formik in React for selecting values from an array of objects

Looking to Add Autocomplete Functionality in Your React Application Using Formik for Seamless Selection of Single and Multiple Values from an Array of Objects? Take a Look at this Code snippet! [see image below] (https://i.stack.imgur.com/ekkAi.png) arra ...

What is the best way to retrieve a particular div element from a webpage?

I'm having trouble retrieving a specific div element (specifically with the attribute id="vung_doc") from a website. Instead of getting just that element, I seem to be getting almost every element on the page. Any ideas on what could be causing this i ...

Error message: "Attempting to assign a value to the 'size' property of an undefined object, despite the fact that

I recently started delving into NodeJS development and have configured Visual Studio Code for JavaScript development in node applications. During a Pluralsight lecture on Objects, the instructor demonstrated creating two scripts, dice.js and program.js, a ...

Gathering all posts related to jagi astronomy

Within my MongoDB database, I have a collection called "post." My goal is to extract all post IDs that are not disabled from this collection. To achieve this, I utilized Jagi Astronomy, a Meteor JS package, to create the schema. However, when I implemented ...

Unable to alter the pagination's selected page color to grey with material UI pagination components

Currently, I am implementing pagination using material UI. While I have successfully changed the page color to white, I am facing difficulty in changing the selected page color to grey. This issue arises because the background color is dark. import React, ...