【问题标题】:Jquery, animate div when mouse is near left sideJquery,当鼠标靠近左侧时为div设置动画
【发布时间】:2013-11-06 05:45:33
【问题描述】:

我正在尝试创建一个当鼠标靠近视口左侧时可见的菜单(比如距离左侧 100 像素,否则显示菜单隐藏)。它应该有点像 Windows 8 的魅力栏。

我已经有了以下内容,但效果并不好。

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');
        });
    }
});

显然我做错了什么。

编辑

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');
        });
    }
});

似乎上面的编辑做得更多,问题是/是隐藏菜单时,动画必须完成。如果不是,而你又用鼠标

也许有人有更流畅的解决方案?

【问题讨论】:

    标签: jquery mouse panel


    【解决方案1】:

    你可以在左右创建一个宽度为100px的div,然后你可以在那个div元素上使用jquery mouseover函数,当菜单可见时那个div会被切换

    这是 jsfiddle 链接 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();
    });
    

    【讨论】:

      【解决方案2】:

      我为此 http://jsfiddle.net/ravikumaranantha/Wtfpx/2/ 创建了一个 jsfiddle,我使用左侧位置而不是可见性来隐藏它

      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');
             }
         });
      

      【讨论】:

      • 简单、整洁、干净、有效!非常感谢!
      猜你喜欢
      • 1970-01-01
      • 2012-01-10
      • 2014-08-24
      • 2017-11-03
      • 2013-04-25
      • 2020-12-27
      • 1970-01-01
      • 1970-01-01
      • 2011-03-03
      相关资源
      最近更新 更多