你的位置:首页 > 软件开发 > Java > JQuery测手速小游戏

JQuery测手速小游戏

发布时间:2016-09-19 21:00:09
(-1)写在前面我用的chrome49,jquery3.0,我得到过399分,信不信由你。 (1)设计思路 两个p元素放在div里,每个p元素的高度和宽度都和div一样,当鼠标放在div上时,第一个p向上移动(更改marginTop值),当鼠标离 ...

(-1)写在前面

我用的chrome49,jquery3.0,我得到过399分,信不信由你。

 (1)设计思路

      两个p元素放在div里,每个p元素的高度和宽度都和div一样,当鼠标放在div上时,第一个p向上移动(更改marginTop值),当鼠标离开div上时,第一个p向下移动。

(2)知识储备

a. :nth-child

  #lol p:nth-child(1),当前元素p相对于他的父元素的所有子元素,如果第一个元素是p则匹配成功。

  #lol :nth-child(1) 相当于#lol *:nth-child(1)

(3) 代码

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset=utf-8>

<script  type="text/javascript" src='/images/loading.gif' data-original="debug-jquery-3.0.0.js"></script>

<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">

<title>为了生活</title>

<style type="text/css">

      *

      {

           margin:0px;

           padding:0;

      }

      body

      {

           position:absolute;

      }

      #lol

      {

           width:400px;

           height:400px;

           position:absolute;

           overflow:hidden;

           cursor:pointer;

      }

      #lol p

      {

           width:400px;

           height:400px;

      }

      #lol p:nth-child(1)

      {   

           background:blue;

      }

      #lol p:nth-child(2)

      {

           background:orange;

      }

</style>

<script type="text/javascript">

      $(function()

      {

           var $lol = $("#lol"),

                 $oneP = $lol.children().first(),

                 $score = $("#score");

           $lol.centerPos();

           $lol.hover(function()

           {

                 $oneP.animate({marginTop:-$oneP.height()},400)

           },function()

           {

                 var number = -parseInt($oneP.css("marginTop"));

                 if(number == $oneP[0].offsetHeight)

                 {

                      number = 0;

                 }

                 $(score).text(number);

                 $oneP.stop(true,false).animate({marginTop:0},400);

           })

      })

      $.fn.centerPos = function()

      {

           var parent;

           this.each(function(index)

           {

                 parent = this.parentNode;

                 if(parent == document.body)

                 {

                      parent = window;

                 }

                 var position = $(this).css("position");

                 if(position == "fixed" || position=="absolute")

                 {

                      $(this).css("left",($(parent).width()-this.offsetWidth)/2+"px")

                               .css("top",($(parent).height()-this.offsetHeight)/2+"px");

                 }

                 else

                 {

                      window.console.error("没有设置有效的position值");

                 }

           })

           return this;

      }   

</script>

</head

<body>

      <div>当前得分:<span id="score">0</span>分</div>

      <div id="lol">

           <p></p>

           <p></p>

      </div>

</body>              

</html>                 

                        

                         


 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:JQuery测手速小游戏

关键词:jquery

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录