这个if...else 条件为什么不成立?又不报错!

新手上路,请多包涵
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        header{
            width: 100%;
            position: fixed;
            top: 0;
        }
        .up{
            height: 55px;
            background-color: darkgreen;
            cursor: pointer;

        }
        .low{
            height: 60px;
            background-color: darkgray;
        }
        .ad{
            height: 340px;
            background-color: #3a945b;
            margin-top: 115px;
        }
        article{
            height: 1000px;
            background-color: orange;
        }
        footer
        {
            height: 320px;
            background-color: #1a1a1a;
        }
    </style>
    <script src="src/javascript/jquery-3.2.1.js"></script>
    <script>
        $(function () {
          var w = $('body').width();
          $('.up').click(function () {
            if(w>'1280px'){
              $('.low').animate({marginTop:200})
            }else{
              $('.low').animate({marginTop:50})
            }
          });
        });
    </script>
</head>
<body>
<header>
    <section class="up"></section>
    <section class="low"></section>
</header>
<div class="ad"></div>
<article></article>
<footer></footer>
</body>
</html>

if 没作用,else可以?

阅读 2k
2 个回答

改成

if(w>1280)

图片描述

>只能进行数值比较,1280px在这里是字符串,不能比较,所以条件触发了else。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题