您的位置:

首页 >

网络编程 >

jQuery的position()方法详解 >

jQuery的position()方法详解

2016-06-08 10:45:17

分类:网络编程

position()方法的定义和用法:此方法获取匹配元素相对某些元素的偏移量。返回的对象包含两个整型属性(top和left)的对象。此方法只对可见元素有效。语法结构:$(selector).position()在教程的开头之所以说是获取匹配元素相对于某些元素的偏移量。很多教程都说方法返回的偏移量是相对于父元素,其实并非完全如此,此方法会将匹配元素以绝对定位方式处理,当然并不是说真的将匹配元素设置为绝对定位。方法的偏移量参考原则如下:1.如果父辈元素中没有采用定位的(position属性值为relative、absolute或者fixed),那么偏移量参考对象为窗口。2.如果父辈元素中有采用定位的,那么偏移量的参考对象为距离它最近的采用定位的元素,实例代码:<!DOCTYPE html><html><head><meta charset=" utf-8"><style type="text/css">*{ margin:0px; padding:0px;}.father{ background-color:green; width:200px; height:200px; padding:50px; margin-bottom:50px; margin-left:100px;}.children{ height:150px; width:150px; background-color:red; line-height:150px; text-align:center;}</style><script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script><script type="text/javascript">$(document).ready(function(){ $(".children").each(function(){ var text; text="left:"+$(this).position().left; text+="top:"+$(this).position().top; $(this).text(text); })})</script></head><body><div class="father" style="position:relative"> <div class="children"></div></div><div class="father"> <div class="children"></div></div></body></html>在以上代码中顶部组合,由于父元素采用的是相对定位,那么获取的偏移量就是相对于父元素的。在底部的组合中,由于父元素没有采用定位,那么偏移量参考对象就是窗口。以上所述就是本文的全部内容了,希望大家能够喜欢。

本文实例讲述了基于canvas实现的钟摆效果。分享给大家供大家参考,具体如下:运行效果截图如下:具体代码如下:<!DOCTYPE html><html> <head> <title>demo</title> <style type="text/css"> body { margin:0; padding:0; } #canvas { border:2px solid gray; box-shadow:0px 0px 2px 2px #494949; } </style> </head> <body> <canvas id="canvas" width="500px" height="500px"></canvas> <script type="text/javascript"> var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); var r = 250; var colorList = "abcdefABCDEF0123456789".split(""); var colorListLength = colorList.length; var color = function() { var _color = "#"; for(var i=0; i<6; i++) { _color += colorList[Math.round(Math.random()*colorListLength)]; } return _color; }; var createArc = function(attrs) { ctx.save(); ctx.beginPath(); ctx.fillStyle = attrs.color || color(); ctx.arc(attrs.x, attrs.y, attrs.r, 0, Math.PI*2); ctx.closePath(); ctx.fill(); ctx.restore(); }; var createLine = function(from, to) { ctx.save(); ctx.beginPath(); ctx.moveTo(from.x, from.y); ctx.lineTo(to.x, to.y); ctx.stroke(); ctx.closePath(); ctx.restore(); } var createAll = function(to) { createArc({x: 250, y: 0, r: 10, color: "black"}); createArc({x: to.x, y: to.y, r: 20, color: "black"}); createLine({x: 250, y: 0}, {x: to.x, y: to.y}); }; var minAngle = 0; var maxAngle = Math.PI; var addAngle = Math.PI/24; var angle = minAngle; var mode = "left"; var interval = setInterval(function() { var y = Math.sin(angle)*r+200; var x = Math.cos(angle)*r+250; switch(mode) { case "left": ctx.clearRect(0, 0, 500, 500); createAll({x: x, y: y}); angle += addAngle; if(angle > maxAngle) { mode = "right"; angle -= addAngle; return; } break; case "right": ctx.clearRect(0, 0, 500, 500); createAll({x: x, y: y}); angle -= addAngle; if(angle < minAngle) { mode = "default"; angle += addAngle; return; } break; case "default": createAll({x: x, y: y}); angle += addAngle; if(angle > maxAngle) { mode = "left"; angle = minAngle; return; } break; } }, 50); </script> </body></html>更多关于js特效相关内容感兴趣的读者可查看本站专题:《jQuery动画与特效用法总结》及《jQuery常见经典特效汇总》希望本文所述对大家JavaScript程序设计有所帮助。

本文实例讲述了JavaScript实现横向滑出的多级菜单效果。分享给大家供大家参考。具体如下:这是一款JavaScript横向滑出的多级菜单,只用鼠标放上就可以动画出现二级菜单,整体设计精美,大气,博客上也可以用哦。运行效果截图如下:在线演示地址如下:http://demo.jb51.net/js/2015/js-hx-show-menu-style-demo/具体代码如下:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>JavaScript横向滑出的多级菜单</title><style>* {padding:0; margin:0}body {font:12px Verdana, Arial, Helvetica}#wrapper {width:750px; padding:25px; margin:0 auto}#leftcolumn {float:left; width:225px}#rightcolumn {float:left; width:525px}.dropdown {display:block; position:relative}.dropdown dt {width:188px; border:2px solid #9ac1c9; padding:8px; font-weight:bold; cursor:pointer; background:url(images/header.gif)}.dropdown .upperdd {border-bottom:none}.dropdown dt:hover {background:url(images/header_over.gif)}.dropdown dd {position:absolute; top:0; overflow:hidden; width:208px; display:none; background:#fff; opacity:0}.dropdown ul {width:204px; border:2px solid #9ac1c9; list-style:none}.dropdown li {display:inline}.dropdown a, .dropdown a:active, .dropdown a:visited {display:block; padding:5px; color:#333; text-decoration:none; background:#eaf0f2; width:194px}.dropdown a:hover {background:#d9e1e4; color:#000}.dropdown .underline {border-bottom:1px solid #b9d6dc}</style><script type="text/javascript">var DDSPEED = 10;var DDTIMER = 15;var OFFSET = -2;var ZINT = 100;function ddMenu(id,d){ var h = document.getElementById(id + '-ddheader'); var c = document.getElementById(id + '-ddcontent'); clearInterval(c.timer); if(d == 1){ clearTimeout(h.timer); c.style.display = 'block'; if(c.maxh && c.maxh <= c.offsetHeight){return} else if(!c.maxh){ c.style.left = (h.offsetWidth + OFFSET) + 'px'; c.style.height = 'auto'; c.maxh = c.offsetHeight; c.style.height = '0px'; } ZINT = ZINT + 1; c.style.zIndex = ZINT; c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER); }else{ h.timer = setTimeout(function(){ddCollapse(c)},50); }}function ddCollapse(c){ c.timer = setInterval(function(){ddSlide(c,-1)},DDTIMER);}function cancelHide(id){ var h = document.getElementById(id + '-ddheader'); var c = document.getElementById(id + '-ddcontent'); clearTimeout(h.timer); clearInterval(c.timer); if(c.offsetHeight < c.maxh){ c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER); }}function ddSlide(c,d){ var currh = c.offsetHeight; var dist; if(d == 1){ dist = Math.round((c.maxh - currh) / DDSPEED); }else{ dist = Math.round(currh / DDSPEED); } if(dist <= 1 && d == 1){ dist = 1; } c.style.height = currh + (dist * d) + 'px'; c.style.opacity = currh / c.maxh; c.style.filter = 'alpha(opacity=' + (currh * 100 / c.maxh) + ')'; if(currh > (c.maxh - 2) && d == 1){ clearInterval(c.timer); }else if(dist < 1 && d != 1){ clearInterval(c.timer); c.style.display = 'none'; }}</script></head><body><div id="wrapper"><div id="leftcolumn"> <dl class="dropdown"> <dt id="one-ddheader" class="upperdd" onmouseover="ddMenu('one',1)" onmouseout="ddMenu('one',-1)">我们首页</dt> <dd id="one-ddcontent" onmouseover="cancelHide('one')" onmouseout="ddMenu('one',-1)"> <ul> <li><a href="#" class="underline">脚本主页</a></li> <li><a href="#" class="underline">网页特效</a></li> <li><a href="#">脚本下载</a></li> </ul> </dd> </dl> <dl class="dropdown"> <dt id="two-ddheader" class="upperdd" onmouseover="ddMenu('two',1)" onmouseout="ddMenu('two',-1)">网站导航</dt> <dd id="two-ddcontent" onmouseover="cancelHide('two')" onmouseout="ddMenu('two',-1)"> <ul> <li><a href="#" class="underline">ASP</a></li> <li><a href="#" class="underline">PHP</a></li> <li><a href="#" class="underline">.NET</a></li> <li><a href="#">网站留言</a></li> </ul> </dd> </dl> <dl class="dropdown"> <dt id="three-ddheader" class="upperdd">最新下载</dt> </dl> <dl class="dropdown"> <dt id="four-ddheader" onmouseover="ddMenu('four',1)" onmouseout="ddMenu('four',-1)">欢迎回来</dt> <dd id="four-ddcontent" onmouseover="cancelHide('four')" onmouseout="ddMenu('four',-1)"> <ul> <li><a href="#" class="underline">欢迎再来</a></li> <li><a href="#">慢走,不送</a></li> </ul> </dd> </dl></div></div></body></html>希望本文所述对大家的JavaScript程序设计有所帮助。

很简单的一段背景颜色时刻发生变化的代码<!doctype html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title> <style> #dv {width: 100px;height: 100px;} </style></head><script> window.onload=function(){ var oDv = document.getElementById('dv'); oDv.style.backgroundColor='red' setInterval(function(){ var arrRandNumber = [ getRandomNumber(0,255), getRandomNumber(0,255), getRandomNumber(0,255) ]; oDv.style.backgroundColor='rgb('+arrRandNumber[0]+','+arrRandNumber[1]+','+arrRandNumber[2]+')' function getRandomNumber(rMin,rMax){ var cha = rMax-rMin; var rand = Math.random(); //return (rMin+Math.round(cha*rand)) } },500) //alert(getRandomNumber(0,255)) };</script><body> <div id="dv"></div></body></html>以上所述就是本文给大家分享的全部内容了,希望能够对大家学习使用javascript有所帮助。

本文实例讲述了jQuery使用empty()方法删除元素及其所有子元素的方法。分享给大家供大家参考。具体实现方法如下:<!DOCTYPE html><html><head><script src="js/jquery.min.js"></script><script>$(document).ready(function(){ $("button").click(function(){ $("#div1").empty(); });});</script></head><body><div id="div1" style="height:100px;width:300px;border:1px solid black;background-color:yellow;">This is some text in the div.<p>This is a paragraph in the div.</p><p>This is another paragraph in the div.</p></div><br><button>Empty the div element</button></body></html>希望本文所述对大家的jQuery程序设计有所帮助。

焦点访谈

最新最热的文章

更多 >

COPYRIGHT (©) 2017 Copyright ©2017 888真人 网站地图

联系我们

827570882

扫描二维码分享到微信