JQuery, an animated div when the mouse is near the left side

I am trying to create a menu that will be visible when the mouse is next to the left viewport (say, up to 100 pixels on the left, otherwise in the "Show" menu). It should look a bit like the Windows 8 firewall.

I already have the following, but actually it does not work.

var mouse_position;
//GET MOUSE POSITION
$(document).mousemove(function (mouse_pointer) {
    //$("body").on("mousemove", function(mouse_pointer) {
    //console.log(mouse_pointer.pageX - $(window).scrollLeft());
    //mouse_position = mouse_pointer.pageX - $(window).scrollLeft();
    mouse_position = mouse_pointer.clientX;

    //console.log(mouse_position);
    if (mouse_position <= 100 && !$("#cms_bar").is(":visible")) {
        //SLIDE IN MENU
        $('#cms_bar').show().animate({
            width: '100px'
        }), 500;
        console.log('menu shown');
    } else if (mouse_position > 100 && $("#cms_bar").is(":visible")) {
        $('#cms_bar').animate({
            width: '0px'
        }, 500, function () {
            $(this).hide();
            console.log('menu hidden');
        });
    }
});

It’s clear that I am doing something wrong.

EDIT

var mouse_position;
//GET MOUSE POSITION
$(document).mousemove(function(mouse_pointer) {
//$("body").on("mousemove", function(mouse_pointer) {
    //console.log(mouse_pointer.pageX - $(window).scrollLeft());
    //mouse_position = mouse_pointer.pageX - $(window).scrollLeft();
    mouse_position = mouse_pointer.clientX;

    //console.log(mouse_position);
    if (mouse_position <= 100 && !$("#cms_bar").is(":visible")) {
        //SLIDE IN MENU
        $('#cms_bar').stop().show().animate({width: '100px'}), 300;
        console.log('menu shown');
    }
    else if (mouse_position > 100 && $("#cms_bar").is(":visible")) {        
        $('#cms_bar').stop().animate({
            width: '0px'
        }, 300, function() {            
            //$(this).hide();
            $(this).css("display","none")
            console.log('menu hidden');
        });
    }
});

It seems that editing is a bit more, the problem was that when you hide the menu, the animation should be completed. If not, and you where again with the mouse <100 then it got stuck and nothing was shown.

Maybe someone has a smoother solution?

+4
source share
2

jsfiddle http://jsfiddle.net/ravikumaranantha/Wtfpx/2/ ,

HTML

<div id="cms_bar">hidden bar</div>

CSS

#cms_bar {
    height:500px;
    width:100px;
    background-color:red;
    position:absolute;
    left:-100px;
    top:0;
}

JavaScript

var mouse_position;
var animating = false;
   //GET MOUSE POSITION
$(document).mousemove(function (e) {
       //$("body").on("mousemove", function(mouse_pointer) {
       //console.log(mouse_pointer.pageX - $(window).scrollLeft());
       //mouse_position = mouse_pointer.pageX - $(window).scrollLeft();
       if (animating) {
           return;
       }
       mouse_position = e.clientX;

       console.log(mouse_position);
       if (mouse_position <= 100) {
           //SLIDE IN MENU
           animating = true;
           $('#cms_bar').animate({
               left: 0,
               opacity: 1
           }, 200, function () {
               animating = false;
           });
           console.log('menu shown');
       } else if (mouse_position > 100) {
           animating = true;
           $('#cms_bar').animate({
               left: -100,
               opacity: 0
           }, 500, function () {
               animating = false;
           });
           console.log('menu hidden');
       }
   });
+4

div 100px, jquery mouseover div, div

jsfiddle link http://jsfiddle.net/8LHFs   html- http://tympanus.net/Blueprints/SlidePushMenus

HTML

<div class="toggle-menu"></div>

CSS

.toggle-menu{
    width: 100px;
    height: 1000px;
    position: fixed;
}

Jquery

$('.toggle-menu').on('mouseover', null, function(){
    $('#showLeft').click();
});
+1

All Articles