簡體   English   中英

CodePen JavaScript代碼在本地不起作用

[英]CodePen javascript code does not work in local

首先,我是javascript新手,我想重用此Codepen中的代碼

    $('a[href*=#]').click(function(){
  return false;
});


var animationEndEvent = "webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend";

var Person = {
  wrap: $('#people'),
  people: [
    {
      name: 'Linda',
      age: 25,
      img: "https://i.imgur.com/QZuGC10.jpg"
    },
    {
      name: 'Lisa',
      age: 20,
      img: "https://i.imgur.com/1EWwp59.jpg"
    },
    {
      name: 'Sandra',
      age: 18,
      img: "https://i.imgur.com/Lu3laIj.jpg"
    },
    {
      name: 'Chloe',
      age: 18,
      img: "https://i.imgur.com/WgYIxhw.png"
    },
    {
      name: 'Alexa',
      age: 23,
      img: "https://i.imgur.com/D0PQegY.png"
    },
    {
      name: 'Maria',
      age: 21,
      img: "https://i.imgur.com/eqd5IhH.jpg"
    },
    {
      name: 'Emma',
      age: 24,
      img: "https://i.imgur.com/4F9NXPo.png"
    },
    {
      name: 'Sara',
      age: 18,
      img: "http://i40.tinypic.com/ofxe21.jpg"
    },
    {
      name: 'Lara',
      age: 22,
      img: "https://i.imgur.com/HMkdN6A.jpg"
    }
  ],   
  add: function(){
    var random =     this.people[Math.floor(Math.random() * this.people.length)];
    this.wrap.append("<div class='person'><img alt='" + random.name + "' src='" + random.img + "' /><span><strong>" + random.name + "</strong>, " + random.age + "</span></div>");
  }
}

var App = {
  yesButton: $('.button.yes .trigger'),
  noButton: $('.button.no .trigger'),
  blocked: false,
  like: function(liked){
    var animate = liked ? 'animateYes' : 'animateNo';
    var self = this;
    Person.add();
    if (!this.blocked) {
      this.blocked = true;           
      $('.person').eq(0).addClass(animate).one(animationEndEvent, function() {
        $(this).remove();
        self.blocked = false;
      });
    }
  }
};

var Phone = {
  wrap: $('#phone'),
  clock: $('.clock'),
  updateClock: function() {
    var date = new Date();
    var hours = date.getHours();
    var min = date.getMinutes();
    hours = (hours < 10 ? "0" : "") + hours;
    min = (min < 10 ? "0" : "") + min;
    var str = hours + ":" + min;
    this.clock.text(str);
  }
}

App.yesButton.on('mousedown', function() {
  App.like(true);
});

App.noButton.on('mousedown', function() {
  App.like(false);
});

$(document).ready(function() {
  Person.people.forEach(function(person){
    new Image().src = person.img; 
  });
  Phone.updateClock();
  setInterval('Phone.updateClock()', 1000);

  Person.add();
  Person.add();
  Person.add();
  Person.add();
});

https://codepen.io/renatorib/pen/extCA )。 但是我不能使其在本地工作。 我插入了jquery(與他使用的同一版本//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js),並且我已經將sass轉換為css。

它看起來一樣,但是它的javascript部分不起作用(沒有照片,沒有按鈕,什么都沒有..)沒有控制台錯誤...我不知道該怎么辦。

PD:對不起,我的英語,希望我能解釋自己

替換它並在底部使用此jQuery版本。

 $('a[href*="#!..."]').click(function(){
  return false;
});

完整回購

如果您按照這些步驟,它應該可以工作。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>your title</title>
    <!-- step 1 add css file-->
    <link href="your-style.css" rel="stylesheet" type="text/css">
    <!-- step 2 add jquery library-->
    <script src="jquery.js"></script>
    <!--step 3 add script that run after dom ready-->
    <script>
    $(function() {
    // dom is ready 
    // your script goes here    
    });
    </script>


</head>
<body>
    <!-- step 4 html structur goes hear. Note that the selectors that you select in script  must be the same in html-->
</body>
</html> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM