JQuery获取元素尺寸、位置及页面滚动事件应用示例


Posted in jQuery onMay 14, 2019

本文实例讲述了JQuery获取元素尺寸、位置及页面滚动事件应用。分享给大家供大家参考,具体如下:

获取元素尺寸

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
 <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
 <script type="text/javascript">
  $(function () {
   var $div=$('.box');
   //盒子内容的尺寸
   console.log($div.width());
   console.log($div.height());
   //盒子内容加上padding的尺寸
   console.log($div.innerWidth());
   console.log($div.innerHeight());
   //盒子的真实尺寸,内容尺寸加上padding加上brder
   console.log($div.outerWidth());
   console.log($div.outerHeight());
   //盒子的真实尺寸加上margin
   console.log($div.outerWidth(true));
   console.log($div.outerHeight(true));
  })
 </script>
 <style type="text/css">
  .box{
   width: 300px;
   height: 200px;
   padding: 20px;
   border: 10px solid #000;
   margin: 20px;
   background-color: gold;
  }
 </style>
</head>
<body>
 <div class="box">
  dd
 </div>
</body>
</html>

获取元素绝对位置

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
 <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
 <script type="text/javascript">
  $(function () {
   var $div=$('.box');
   //获取元素绝对位置
   var oPos=$div.offset();
   console.log(oPos);
   $div.click(function () {
    // alert(oPos.left);
    document.title=oPos.left+"|"+oPos.top;
   })
  })
 </script>
 <style type="text/css">
  .box{
   width: 200px;
   height: 200px;
   background-color: #f6b544;
   margin: 50px auto 0;
  }
 </style>
</head>
<body>
 <div class="box">
 </div>
</body>
</html>

主要就是offset()函数

加入购物车动画

设置一个按钮,一个购物车框,一个小方框(隐藏)。点击按钮之后,小方框的位置从按钮以animate动画的形式放到购物车框,购物车的数量加一:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
 <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
 <script type="text/javascript">
  $(function () {
   var $chart=$('.chart');
   var $count=$('.chart em');
   var $btn=$('.add');
   var $point=$('.points');
   var $w01=$btn.outerWidth();
   var $h01=$btn.outerHeight();
   $btn.click(function () {
    var oPos01=$btn.offset();
    var oPos02=$chart.offset();
    $point.css({left:oPos01.left+parseInt($w01/2)-8,top:oPos01.top+parseInt($w01/2)-8}).show();/*移动到购物车按钮上,然后show*/
    $point.animate({left:oPos02.left+parseInt($w01/2)-8,top:oPos02.top+parseInt($w01/2)-8},800,function () {
     $point.hide();
     var iNum=$count.html();/*读em里的信息*/
     $count.html(parseInt(iNum)+1);/*转换成int类型然后加一*/
    });
   })
  });
 </script>
 <style type="text/css">
  .chart{
   width: 150px;
   height: 50px;
   border: 2px solid #000;
   text-align: center;
   line-height: 50px;
   float: right;
   margin-right:100px ;
   margin-top: 100px;
  }
  .chart em{
   font-style: normal;
   color: red;
   font-weight: bold;
  }
  .add{
   width: 100px;
   height: 50px;
   border: 0;/*去掉边框*/
   background-color: green;
   color: #fff;
   float: left;
   margin-top: 300px;
   margin-left: 300px;
  }
  .points{
   width: 16px;
   height: 16px;
   background-color: red;
   position: fixed;/*固定定位,就是相对于页面位置的定位*/
   left: 0;
   top: 0;
   display: none;/*把小红点藏起来*/
   z-index: 999;/*这样设置小红点就能盖住其他元素*/
  }
 </style>
</head>
<body>
 <div class="chart">购物车<em>0</em></div>
 <input type="button" name="" value="加入购物车" class="add">
 <div class="points"></div>
</body>
</html>

感兴趣的朋友可以使用在线HTML/CSS/JavaScript代码运行工具 http://tools.3water.com/code/HtmlJsRun 测试上述代码运行效果。

希望本文所述对大家jQuery程序设计有所帮助。

jQuery 相关文章推荐
vue中如何引入jQuery和Bootstrap
Apr 10 jQuery
jQuery滑动到底部加载下一页数据的实例代码
May 22 jQuery
bootstrap+jQuery 实现下拉菜单中复选框全选和全不选效果
Jun 12 jQuery
jquery鼠标悬停导航下划线滑出效果
Sep 29 jQuery
jQuery实现使用sort方法对json数据排序的方法
Apr 17 jQuery
jQuery中的for循环var与let的区别
Apr 21 jQuery
手写简单的jQuery雪花飘落效果实例
Apr 22 jQuery
jQuery实现的监听导航滚动置顶状态功能示例
Jul 23 jQuery
jQuery实现轮播图及其原理详解
Apr 12 jQuery
jQuery选择器之层次选择器用法实例分析
Feb 19 jQuery
JQuery样式操作、click事件以及索引值-选项卡应用示例
May 14 jQuery
JS 遍历 json 和 JQuery 遍历json操作完整示例
Nov 11 jQuery
JQuery animate动画应用示例
May 14 #jQuery
jquery实现选项卡切换代码实例
May 14 #jQuery
JQuery样式操作、click事件以及索引值-选项卡应用示例
May 14 #jQuery
使用jQuery如何写一个含验证码的登录界面
May 13 #jQuery
JQuery特殊效果和链式调用操作示例
May 13 #jQuery
JQuery的加载和选择器用法简单示例
May 13 #jQuery
JQuery事件委托原理与用法实例分析
May 13 #jQuery
You might like
PHP 采集获取指定网址的内容
2010/01/05 PHP
解析Linux下Varnish缓存的配置优化
2013/06/20 PHP
PHP借助phpmailer发送邮件
2015/05/11 PHP
PHP魔术方法以及关于独立实例与相连实例的全面讲解
2016/10/18 PHP
PHP实现的各类hash算法长度及性能测试实例
2017/08/27 PHP
PHP strripos函数用法总结
2019/02/11 PHP
laravel框架 api自定义全局异常处理方法
2019/10/11 PHP
JS 拼图游戏 面向对象,注释完整。
2009/06/18 Javascript
javascript中利用数组实现的循环队列代码
2010/01/24 Javascript
JS中setTimeout()的用法详解
2013/04/14 Javascript
jqeury-easyui-layout问题解决方法
2014/03/24 Javascript
js 实现浏览历史记录示例
2014/04/20 Javascript
JavaScript结合AJAX_stream实现流式显示
2015/01/08 Javascript
基于HTML5上使用iScroll实现下拉刷新,上拉加载更多
2016/05/21 Javascript
从零开始学习Node.js系列教程之SQLite3和MongoDB用法分析
2017/04/13 Javascript
vue2.0s中eventBus实现兄弟组件通信的示例代码
2017/10/25 Javascript
javascript标准库(js的标准内置对象)总结
2018/05/26 Javascript
vue路由事件beforeRouteLeave及组件内定时器的清除方法
2018/09/29 Javascript
详解基于React.js和Node.js的SSR实现方案
2019/03/21 Javascript
Jquery使用each函数实现遍历及数组处理
2020/07/14 jQuery
Element Backtop回到顶部的具体使用
2020/07/27 Javascript
Vue3+elementui plus创建项目的方法
2020/12/01 Vue.js
[02:36]DOTA2英雄基础教程 帕格纳
2014/01/20 DOTA
python-docx修改已存在的Word文档的表格的字体格式方法
2018/05/08 Python
python for循环输入一个矩阵的实例
2018/11/14 Python
java判断三位数的实例讲解
2019/06/10 Python
使用pycharm在本地开发并实时同步到服务器
2019/08/02 Python
python selenium操作cookie的实现
2020/03/18 Python
TripAdvisor日本:全球领先的旅游网站
2019/02/14 全球购物
美国相机和电子产品零售商:Beach Camera
2020/11/26 全球购物
请解释一下webService? 如何用.net实现webService
2014/06/09 面试题
汽车维修与检测专业应届生求职信
2013/11/12 职场文书
村捐赠仪式答谢词
2014/01/21 职场文书
年度献血先进个人事迹材料
2014/02/14 职场文书
Spring Boot 实现敏感词及特殊字符过滤处理
2021/06/29 Java/Android
天谕手游15杯全调酒配方和调酒券的获得方式
2022/04/06 其他游戏