源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

js实现砖头在页面拖拉效果

  • 时间:2022-08-28 10:22 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:js实现砖头在页面拖拉效果
用js实现一个砖头在页面,但鼠标点击拖动时,砖头在页面上形成拖拉效果: 刚开始时: [img]http://files.jb51.net/file_images/article/201611/201611290008625.jpg?20161029016[/img] 鼠标点击拖动后: [img]http://files.jb51.net/file_images/article/201611/201611290026088.jpg?20161029032[/img] 实现代码:
<html>
  <head>
    <meta charset="utf-8">
    <style type="text/css">
   *{
     margin:0px;
     padding:0px;
    }
  #zhuantou{
    width:120px;
    height:60px;
    background-image:url(1.jpg);
    position:fixed;
    left:100px;
     top:50px;
   }
   </style>
 <body>
   <div id="zhuantou">
   </div>
    <script>
    var zt=document.querySelector("#zhuantou");
    var isPressed=false;
    var offsetX=0;
    var offsetY=0;
    zt.onmousedown=function(event){
         isPressed=true;
         this.style.cursor="move";
         offsetX=event.offsetX;
        offsetY=event.offsetY;
    };
    zt.onmouseup=function(){
       isPressed=false;
       this.style.cursor="default";
    };
    zt.onmousemove=function(event){
      if(!isPressed){
          return;
       }
      zt.style.left=(event.clientX-offsetX)+"px";
     zt.style.top=(event.clientY-offsetX)+"px";
    };
 </script>
 </body>
</html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部