Tips for designing a sleek and seamless floating button

I attempted to implement a floating button feature that remains in view while smoothly flowing using jQuery. I followed a tutorial I found online to create this for my website. The tutorial can be accessed at this link. However, after following all the steps outlined in the tutorial, it seems that something went wrong as the functionality is not working as intended.

Below is the code snippet:

$().ready(function() {
    var $scrollingDiv = $(".floatBtn");

    $(window).scroll(function(){            
        $scrollingDiv
            .stop()
            .animate({"marginTop": ($(window).scrollTop() + 30) + "px"}, "slow" );          
    });
});
.floatBtn{
position: absolute;
float: right;
right: 0;
text-align: right;
z-index: 5;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<p>
Lorem ipsum dolor sit amet, an has dicat aperiri delicata, te maiorum omittantur theophrastus vim, ut quot sapientem reprehendunt eam. Exerci omnium eu eum, legendos explicari sed ad. Apeirian mandamus vix et. Tollit dolorem has at, mod...lorem labore consequuntur sea, ne debet sententiae constituam pri.

Utamur adipisci sadipscing has ex. Tantas voluptatibus ius ei, vel definiebas signiferumque at. Lorem voluptaria te vel, cum ne consequuntur necessitatibus. Ex suas probo propriae cum. Duo ullum abhorreant dissentiet no. Fuisset consequat vim no.
...
Atqui invenire vim in, no usu possim tractatos principes. Invidunt probatus est te, eos ea error sententiae, id alterum nostrum sea. Mollis moderatius omittantur has ex, falli movet te has. At viris commodo deserunt quo.

Eu tale putent referrentur vix, cu quo admodum voluptatum appellantur. Usu ne ignota prodesset, ipsum quando an eos, dolores abhorreant an quo. Cu assum consequat ius, lobortis consequat eos no, ne mea suas congue viderer. Semper latine eripuit in quo. Vel...

In the scroll function, when scrolling down, the floating button also scrolls further down instead of staying within the viewing area, which is not the desired behavior. Any help or suggestions would be greatly appreciated. Thank you in advance!

Answer №1

To ensure a button remains visible at all times, it is important to use the position fixed property. By positioning the element relative to the viewport, the button will be constantly visible on the screen. Here's an example of how you can achieve this without using jquery:

.floatBtn{
   display: block;
   position: fixed;
   width: 200px;
   height:200px;
   color: red;
   border: 3px solid black;
   top: 20px;
 }

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

Deciphering unidentified Json data

Having some trouble with an error in my note taker app built using expressjs. Everything was working fine until I tried to save a new note and it's throwing this error: SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse () Here&apos ...

When the webpage first loads, the CSS appears to be broken, but it is quickly fixed when

Whenever I build my site for the first time, the CSS breaks initially, but strangely fixes itself when I press command + s on the code. It seems like hot reloading does the trick here. During development, I can temporarily workaround this issue by making ...

Creating an array of form input names using JavaScript within the HTML input tag

I have a two part question that I'm hoping someone can help me with. There was a similar question asked recently that included information about this particular type of array in PHP, but unfortunately, I can't seem to locate it at the moment. 1. ...

The use of a jQuery AJAX call results in the garbled text being sent when two or more question marks appear consecutively

I have a simple messaging system on my website. Occasionally, and very rarely (making it difficult to replicate the issue), jQuery seems to be altering the message text. Here is an example of when the error occurs: *wirklich? sehr cool :) und wie organ ...

Adjustments made to the size of the h1 font

I am perplexed by the fact that these two h1 tags have different font sizes, even though they have identical properties and declarations. From what little knowledge I have, it seems like it may be related to inheritance; considering one h1 tag is nested an ...

Saving JSON data into an HTML element using Handlebars templating

Is there a way to save the entire JSON object within an HTML element as a data attribute? let a = {name : "sample", age : "34"} $.find('#someDiv').data('adata', a); Is it possible to achieve the same result using Handlebars when creat ...

Difficulty with incorporating jQuery into Zend Studio/Framework

Struggling to create a simple Zend Framework application that can utilize jQuery. Currently working in Zend Studio 7.1.1, I have added the "ZendX" folder to the library directory and downloaded the jQuery lightness theme into the "js" public folder. The s ...

I'm receiving the error message "app.get is not a function" when using Express.js. What could be

Having trouble understanding why I am getting an error: app.get is not a function in the upload.js file with the provided code snippet. In my index.js file, I have set up everything and exported my app using module.exports = app. I have also used app.set( ...

Why is it that @font-face fails to function properly even after being defined correctly?

Here's the @font-face code I've been using: @font-face { font-family: "NotesEsa-Bold"; src: url("C:\Users\test\Desktop\Webpage\Fonts\NotesEsaBol.ttf") format("truetype"); /* Safari, Android, iOS */ font-we ...

Using a combination of PHP and JQuery, I noticed that adding an IF statement within the document ready function

In the (document).ready function, the code below is used to properly display the order form: <?php if (isset($_POST['preview-order'])) { //IF PREVIEW FORM ?> $("#cam-order-preview").show('slow'); <?ph ...

The functionality of ngModel is not functioning properly on a modal page within Ionic version 6

Currently I am working on an Ionic/Angular application and I have encountered a situation where I am attempting to utilize ngModel. Essentially, I am trying to implement the following functionality within my app: <ion-list> <ion-item> <ion ...

What is the best way to populate a table with form text fields using jQuery?

I have a scenario where I need to toggle between two forms. The initial form #form1 includes input fields for month and year, along with a search button. The secondary form #form2 consists of a table that needs to be populated based on the month and year s ...

Multiple instances of jQuery file being loaded repeatedly

Having an issue with the jQuery file (jquery-1.11.3.min.js) loading multiple times. It seems that other jQuery files in the index.html page might be causing this. Any advice on how to resolve this would be greatly appreciated. <script type="text/javasc ...

Tips for iterating through nested objects with a for loop

Struggling with validations in an Angular 5 application? If you have a form with name, email, gender, and address grouped under city, state, country using FormGroupname, you might find this code snippet helpful: export class RegistrationComponent implemen ...

The mobile functionality of the stylesheet is not functioning properly

Recently, I came across an issue with a stylesheet designed for mobile devices that contains the following code: /* Smartphones (portrait) ----------- */ @media only screen and (max-width : 320px) { /* Styles */ } Surprisingly, this code failed to work ...

What causes the variable to be undefined in the method but not in the constructor in Typescript?

I am currently working on an application using AngularJS 1.4.9 with Typescript. In one of my controllers, I have injected the testManagementService service. The issue I'm facing is that while the testManagementService variable is defined as an object ...

Experiencing an inexplicable blurring effect on the modal window

Introduction - I've implemented a feature where multiple modal windows can be opened on top of each other and closed sequentially. Recently, I added a blur effect that makes the background go blurry when a modal window is open. Subsequently opening an ...

The expiration time and date for Express Session are being inaccurately configured

I'm experiencing an issue with my express session configuration. I have set the maxAge to be 1 hour from the current time. app.use( session({ secret: 'ASecretValue', saveUninitialized: false, resave: false, cookie: { secure ...

Angular Build Showing Error with Visual Studio Code 'experimentalDecorators' Configuration

Currently experiencing an issue in VSC where all my typescript classes are triggering intellisense and showing a warning that says: "[ts] Experimental support for is a feature that is subject to change in a future build. Set the 'experimentalDeco ...

SVG Opacity Transitions seem to ignore the constraints of CSS rules and bounce chaotically beyond their designated boundaries

I'm currently working on implementing a hover effect for an SVG rect embedded in HTML. The transition I've set up is not smooth, and the opacity values seem to be inconsistent during the animation. My browser of choice is Firefox. The rect I&apo ...