Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 脚本不在else语句中输入_Javascript_Jquery_Css - Fatal编程技术网

Javascript 脚本不在else语句中输入

Javascript 脚本不在else语句中输入,javascript,jquery,css,Javascript,Jquery,Css,我的页面右侧有一个表格(带有id=“efficacia”)。我编写了这个JS脚本,以便左键/右键单击它 JavaScript $("#efficacia").click(function() { var posizione = $("#efficacia").position(); if(posizione.right != 0) { $(this).animate({ right: '0' }, 1000); }

我的页面右侧有一个表格(带有
id=“efficacia”
)。我编写了这个
JS
脚本,以便左键/右键单击它

JavaScript

$("#efficacia").click(function() {
    var posizione = $("#efficacia").position();

    if(posizione.right != 0) {
        $(this).animate({
            right: '0'
        }, 1000);
    } else {
        $(this).animate({
            right: '-202'
        }, 1000);
    }
});
CSS

#efficacia {
    position: fixed;
    right: -202px;
    top: 200px;
    cursor: pointer;
    z-index: 100;
}
我的脚本在第一次“单击”时运行良好,然后当我再次单击时,它不会在202px的右侧显示。该函数似乎没有进入else状态。右侧不存在。只有顶部和左侧。


将您的条件基于
left
属性,它将起作用您的代码在右侧出现错误:'-202'。您必须指定右侧:'-202px'

以下是更新的代码:

$("#efficacia").click(function() {
   var right = $(this).css('right');

   if(right != "0px") {
       $(this).animate({
        right: '0px'
       }, 1000);
   } else {
       $(this).animate({
           right: '-202px'
       }, 1000);
   }
});

就是这样

如果要获取所有角点的位置,可以使用
getBoundingClientRect()
。它将返回div的底部、右侧、左侧、顶部、宽度和高度。当然,它将返回从左角开始的值

var boundingBox = $(this).get(0).getBoundingClientRect();
console.log(boundingBox.right);
不返回
右侧属性

您可以获得正确的
css属性,以便:

$("#efficacia").click(function() {
    var right = parseInt($(this).css('right'));

    if(right != 0) {
        $(this).animate({
            right: '0'
        }, 1000);
    } else {
        $(this).animate({
            right: '202'
        }, 1000);
    }
});
请参见

jQuery.position()
不返回样式的值。对。只返回样式

Object{top:somVal,left:somVal}
因此,如果你想获得正确的值,那么从style属性中获取它

乙二醇-


尝试记录
posizione.right
-就像
console.log(posizione.right)
它不起作用。脚本的行为方式相同way@Domenico这不是一个解决方案,他问的是记录了什么值。这不是一个解决方案。。。您需要查看
posizione.right
的值,以了解条件不起作用的原因。。。因此,我提供的行将值记录到浏览器的控制台,我已经更新了代码。希望它能很好地解决你的问题。
位置。正确的
不存在<代码>位置
只给出顶部和左侧位置。我不是在说位置。右侧。我提到动画函数中的css属性,如右图所示:-202px,其中如他所说的右图:-202。知道了。在没有完全通过的情况下不要否决投票。是的,关于失败的单元,你是对的,但是这个答案没有给出(整体)解决方案,因为
位置。right
无效。你自己测试一下,你就会明白。不要在没有彻底检查的情况下给出答案;)当然,但这将在一定程度上改善解决他的问题。那么,有什么必要否决这个答案呢?我已经更新了密码。希望它能很好地解决你的问题。
  $("#efficacia").click(function() {
       // var posizione = $("#efficacia").position();//here right is undefined
     var right=this.style.right.replace("px", "") * 1;//fetch value of right from style
        if(right != 0) {
            $(this).animate({
                right: '0' 
            }, 1000);
        } else {
            $(this).animate({
                right: '202'
            }, 1000);
        }
    });