Article From:https://segmentfault.com/q/1010000012136147
Question:
<!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 It doesn’t work, else can?

Answer 0:

Change into

if(w>1280)

Answer 1:

>Only numeric comparisons can be made. 1280px is a string here and cannot be compared, so the condition triggers else.

Leave a Reply

Your email address will not be published. Required fields are marked *