Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i create a Floating Button Using jQuery and CSS?

Tags:

jquery

.net

css

var name = "#floatMenu";
var menuYloc = null;

$(document).ready(function () {
    menuYloc = parseInt($(name).css("top").substring(0, $(name).css("top").indexOf("px")))

    $(window).scroll(function () {
        var offset = menuYloc + $(document).scrollTop() + "px";
        $(name).animate({ top: offset }, { duration: 500, queue: false });
    });

How can i create a floating button using jQuery and CSS?

like image 297
user1240045 Avatar asked Mar 22 '12 12:03

user1240045


2 Answers

You could achieve a floating button much simpler with css position: fixed; top: 100px; left: 100px. This would always be visible when you scroll the page and on the same spot.

like image 193
slash197 Avatar answered Oct 06 '22 17:10

slash197


There are two options you can choose from.

CSS or JavaScript (like you attempt in your snippet)

Chris Coyier has explained this already, so I'm just going to link to his page: http://css-tricks.com/scrollfollow-sidebar/

Using the CSS method, you won't get any juicy animations.

like image 43
martinjlowm Avatar answered Oct 06 '22 17:10

martinjlowm