Trigger fixed element to appear/disappear when scrolling using JavaScript

I've tried multiple solutions, but I can't seem to get this functionality to work properly on my website. I'm attempting to create a "schedule a demo" button similar to the one on www.bamboohr.com

Currently, I have the following html, css, and javascript code. It works fine on jsfiddle, but when I add it to my WordPress child theme, it doesn't work. I added the html in the header.php of my child theme, the css in a custom stylesheet, and the javascript linked in the header.php file.

Can anyone help me figure out why this isn't working?

This code snippet goes right before the main content <div>:

    <div class="askFloater">
      <a href="http://workforcematters.hubicle.com" target="_blank">
        <i class="icon-question-sign"></i><br />
        Ask Us Anything
      </a>
    </div>

The following script loads in the <head> section:

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ></script>
    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
    <script src="http://dev.workforcematters.com/wp-content/themes/swatch-child-theme/javascripts/askFloater.js"></script>

This is the css in the stylesheet:

.askFloater {
display: none;
position: fixed;
bottom: 20px;
right: 0;
width: 70px;
height: 100px;
text-align: center;
background-color: #3498db;
border-radius: 3px 0 0 3px;
padding: 16px 16px 0;
z-index: 1;
}

.askFloater a {
color: #fff;
font-size: 18px;
text-transform: lowercase;
font-family: 'Muli', 'Lucida Grande', sans-serif;
}

.askFloater:hover {
background-color: #4ea5db;
}

And finally, this is the content of the "askFloater.js" file:

$(document).scroll(function () {
var y = $(this).scrollTop();
if (y > 800) {
    $('.askFloater').fadeIn();
} else {
    $('.askFloater').fadeOut();
}

});

Answer №1

UPDATE: You may want to consider integrating this code snippet with your current $(window).scroll() function in the theme's JavaScript:

// This function modifies the fixed header's class based on scrolling behavior
function modifyHeaderOnScroll() {
    var header = $(".navbar-inner");
    $(window).scroll(function() {
        var scroll = $(window).scrollTop();

        if (scroll >= 230) {
            header.addClass("navbar-scrolled");
        } else {
            header.removeClass("navbar-scrolled");
        }

        if (scroll > 800) {
            $('.askFloater').fadeIn();
        } else {
            $('.askFloater').fadeOut();
        }
    });
}

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

Vue-loader component experiencing issues with applying dynamic CSS styling

I'm working with a Vue Component and I want to enhance it by binding a color to a specific message. Initially, I attempted to achieve this using a binding like :style="styles" However, I encountered the following error: The property or method "st ...

Querying jQuery - how can I accomplish this task?

I have been experimenting with the modal forms available in jQueryTools. I am interested in implementing these modal forms on one of my web pages, taking inspiration from the example provided here: However, I need to make some modifications to the example ...

Conceal User Interface Angular Tabulation

I'm working on setting up UI Bootstrap tabs, and for reference you can find the plunker here. My goal is to hide the tabs since I will be utilizing a button to switch between them. Despite adding the following CSS class to my file, the styling is not ...

What is the best way to transfer a string to a different Vue component?

Within my project, I have a masterData.js file that serves as a repository for my master data. This file retrieves data from my mongo db and distributes it to various components of the project. To facilitate this process, I have implemented a function with ...

Limitations on jQuery Slider

I have implemented a jQuery Slider on my website to allow users to choose how much money they want to pay for upgrading their server. I want the slider to display in its entirety, but with a restriction that prevents users from sliding it below the amount ...

The pages are constantly showing an error message that reads, "There is a problem with the SQL syntax in your code."

I am having trouble with my login page as it is displaying an error on my index.php file. Below is the code snippet from my index.php: <?php include('supsrwk_epenyenggaraan.php'); ?> <?php if (!function_exists("GetSQLValueString")) { f ...

Alter the style attribute using JavaScript

I have a calendar table row that needs to be displayed or hidden based on the month. HTML: <tr id="bottomrow" class="week" valign="top" style="display:none;"> <td class="day" id="d36"> <div class="daynumb" id="x36">& ...

Having trouble grasping the inner workings of code while iterating through a JSON array in ReactJS

Currently, I am immersed in a school project that requires me to develop a simple CRUD web application. After weighing my options, I decided to utilize Spring Boot + ReactJS for this endeavor. The progress has been smooth so far, but I must admit that part ...

Click on a link to open it in the current tab with customized headers

In my Angular project, I am attempting to open a link by clicking a button that redirects to the specified URL using the following code: window.open(MY_LINK, "_self"); However, in this scenario, I also need to include an access token in the header when t ...

Looking for consistent vertical and horizontal scrolling behavior in a React project

I am in need of assistance as I lack the necessary expertise in ReactJs to transform this design. Currently, it is written in Jquery but I would like guidance on how to recreate it in ReactJs. Specifically, I want the left div with the topic for each row t ...

The $http.get in AngularJS is causing a problem by returning undefined and $http() is not being recognized

I am currently working on an app that is designed to load and display data dynamically from a database using AngularJS. However, I have been encountering errors when trying to access my API using both $http() and $http.get(). The specific errors I am recei ...

Is there a way to programmatically click on an href link in Firefox? The method that is typically used in Internet Explorer

http://jsfiddle.net/HVGre/1/ - check out this test link. I have a link in my HTML code that needs to be clickable dynamically. While it functions well with the .click() method in Internet Explorer, it fails in Firefox. Unfortunately, changing the link to ...

When attempting to make an AJAX call using the console, an error was encountered stating that the function $.ajax is not available

Currently experimenting with an ajax call from my console to a local server, but encountering an error: VM4460:1 Uncaught TypeError: $.ajax is not a function(…) Here's the code snippet causing the issue: url = 'http://localhost:8080/testform ...

Content Management System editing plan

Have you ever wondered if there is a structured approach to editing content management systems like Wordpress and Joomla? When it comes to editing aspects such as CSS and JavaScript, what steps do you usually take? Personally, I have been creating files l ...

Encountering difficulty importing TypeScript files dynamically within a Deno executable

When attempting to import a file from aws in an exe using its public link based on user input, I am facing difficulties For example, I generated my exe with the command below deno compile --allow-all main.ts Users execute this exe using commands like ./e ...

What is the best way to apply typography theme defaults to standard tags using Material-UI?

After reading the documentation on https://material-ui.com/style/typography/ and loading the Roboto font, I expected a simple component like this: const theme = createMuiTheme(); const App = () => { return ( <MuiThemeProvider theme={theme}> ...

Automating the deployment of a vue.js app using GitLab CI/CD pipeline to deploy to

Currently experiencing an issue with the pipelines involving my YAML file that automates deployment of a Vue app to Firebase. Despite including scripts in the file and setting up the Environment variable FIREBASE_TOKEN on GitLab, pushing the code to a GitL ...

Begin the div at a width of 0 pixels and expand it towards the right

My current project involves opening a row of blocks with an animation. The desired outcome is to transition from 2 blocks to 3 blocks by clicking a button. Specifically, the block that needs to be added should appear in the middle and fade in from left to ...

What causes the function execution to not be delayed by setTimeout?

function attemptDownloadingWebsite(link) { iframe = document.getElementById('downloadIFrame'); iframe.src = link; setTimeout(removeFile(link), 25000); } This is the remove file function: function removeFile(link){ $.ajax ...

Triggering the Bootstrap Carousel to begin when the window has finished loading, following the completion of the loading

I created a portfolio with a loading GIF that displays until the page is fully loaded. However, I encountered an issue where the Bootstrap Carousel starts sliding images before everything is loaded. This causes the carousel to already display image 5 or 6 ...