Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.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 使用整数中的百分比_Javascript_Jquery - Fatal编程技术网

Javascript 使用整数中的百分比

Javascript 使用整数中的百分比,javascript,jquery,Javascript,Jquery,我已经问了自己很长一段时间,如何在一个整数中使用百分比。实际上就像在CSS中声明高度和宽度一样 [示例] $(window).scroll(function() { var scrollVal = $(window).scrollTop(); if(scrollVal >= 472.141) { $("nav").css("position", "fixed")

我已经问了自己很长一段时间,如何在一个整数中使用百分比。实际上就像在CSS中声明高度和宽度一样

[示例]

$(window).scroll(function() {
                    var scrollVal = $(window).scrollTop();

                    if(scrollVal >= 472.141) {
                        $("nav").css("position", "fixed");
                        $("nav").css("padding-top", "0");
                        $("nav ul").css("background-image", "linear-gradient(rgba(230, 230, 230, 1) 5%, rgba(230, 230, 230, 0))");
                    } else {
                        $("nav").css("position", "relative");
                        $("nav").css("padding-top", "35%");
                        $("nav ul").css("background-image", "linear-gradient(rgba(250, 250, 250, 0), rgba(250, 250, 250, 1) 95%)");
                    }
                });
这里我希望整数->ifscrollVal>=472.141->472.141中的百分比应该是35%->ifscrollVal>=35%

这会很酷,因为scrollVal可以与屏幕的大小相关


提前谢谢你

您必须创建自己的函数来将百分比值转换为相应的像素数:

常量高度百分比=百分比=>window.innerHeight*百分比/100; console.logheightPercent50; 控制台。对数高度百分比10;
console.logheightPercent100 解决这个问题的一种方法是根据您的位置添加css类。例如:

$(window).scroll(function() {
    var scrollVal = $(window).scrollTop();

        if(scrollVal >= 472.141) {
            $("nav").addClass('class0');
            $("nav").removeClass('class1');
        } else {
            $("nav").addClass('class1');
            $("nav").removeClass('class0');
        }
});

什么的35%?JavaScript怎么知道它指的是什么?scrollVal/$'body'。高度>0.35?我不太清楚,因为我还在学习编码。。。