如:elem.style.height 或者 elem.style.height = '100px', 这里要注意的是设置任何几何属性必须明确尺寸单位(如px),同时任何几何属性返回的是表示样式的字符串而非数值(如'100px'而非100)。另外像elem.style.height这样的操作,也能获取元素style属性中设置的样式值,如果你把样式统一放在css文件中,上述方法只会返回一个空串。为了获取元素真实、最终的样式,书中给出了一个函数
复制代码 代码如下:
//get a style property (name) of a specific element (elem)
function getStyle(elem, name) {
// if the property exists in style[], then it's been set
//recently (and is current)
if(elem.style[name]) return elem.style[name];
//otherwise, try to use IE's method
else if (elem.currentStyle) return elem.currentStyle[name];
//Or the W3C's method, if it exists
else if (document.defaultView && document.defaultView.getComputedStyle) {
//it uses the traditional 'text-align' style of rule writing
//instead of textAlign
name = name.replace(/[A-Z]/g, '-$1');
name = name.toLowerCase();
//get the style object and get the value of the property ( if it exists)
var s = document.defaultView.getComputedStyle(elem,'');
return s && s.getPropertyValue(name);
} else return null;
}
理解如何获取元素的在页面的位置是构造交互效果的关键。先复习下css中position属性值的特点。
static:静态定位,这是元素定位的默认方式,它简单的遵循文档流。但元素静态定位时,top和left属性无效。
relative:相对定位,元素会继续遵循文档流,除非受到其他指令的影响。top和left属性的设置会引起元素相对于它的原始位置进行偏移。
absolute:绝对定位,绝对定位的元素完全摆脱文档流,它会相对于它的第一个非静态定位的祖先元素而展示,如果没有这样的祖先元素,它的定位将相对于整个文档。
fixed:固定定位把元素相对于浏览器窗口而定位。它完全忽略浏览器滚动条的拖动。
作者封装了一个跨浏览器的获取元素页面位置的函数
其中有几个重要元素的属性:offsetParent,offsetLeft,offsetTop(可直接点击到Mozilla Developer Center的相关页面)
复制代码 代码如下:
//find the x (horizontal, Left) position of an element
function pageX(elem) {
//see if we're at the root element, or not
return elem.offsetParent ?
//if we can still go up, add the current offset and recurse upwards
elem.offsetLeft + pageX(elem.offsetParent) :
//otherwise, just get the current offset
elem.offsetLeft;
}
//find the y (vertical, top) position of an element
function pageY(elem) {
//see if we're at the root element, or not
return elem.offsetParent ?
//if we can still go up, add the current offset and recurse upwards
elem.offsetTop + pageY(elem.offsetParent) :
//otherwise, just get the current offset
elem.offsetTop;
}
我们接着要获得元素相对于它父亲的水平和垂直位置,使用元素相对于父亲的位置,就可以为DOM增加额外的元素,并相对定位于它的父亲。
复制代码 代码如下:
//find the horizontal position of an element within its parent
function parentX(elem) {
//if the offsetParent is the element's parent, break early
return elem.parentNode == elem.offsetParent ?
elem.offsetLeft :
// otherwise, we need to find the position relative to the entire
// page for both elements, and find the difference
pageX(elem) - pageX(elem.parentNode);
}
//find the vertical positioning of an element within its parent
function parentY(elem) {
//if the offsetParent is the element's parent, break early
return elem.parentNode == elem.offsetParent ?
elem.offsetTop :
// otherwise, we need to find the position relative to the entire
// page for both elements, and find the difference
pageY(elem) - pageY(elem.parentNode);
}
元素位置的最后一个问题,获取元素当对于css定位(非static)容器的位置,有了getStyle这个问题很好解决
复制代码 代码如下:
//find the left position of an element
function posX(elem) {
//get the computed style and get the number out of the value
return parseInt(getStyle(elem, 'left'));
}
//find the top position of an element
function posY(elem) {
//get the computed style and get the number out of the value
return parseInt(getStyle(elem, 'top'));
}
接着是设置元素的位置,这个很简单。
复制代码 代码如下:
//a function for setting the horizontal position of an element
function setX(elem, pos) {
//set the 'left' css property, using pixel units
elem.style.left = pos + 'px';
}
//a function for setting the vertical position of an element
function setY(elem, pos) {
//set the 'top' css property, using pixel units
elem.style.top = pos + 'px';
}
再来两个函数,用于调准元素的当前位置,在动画效果中很实用
复制代码 代码如下:
//a function for adding a number of pixels to the horizontal
//position of an element
function addX(elem, pos) {
//get the current horz. position and add the offset to it
setX(elem, posX(elem) + pos);
}
//a function that can be used to add a number of pixels to the
//vertical position of an element
function addY(elem, pos) {
//get the current vertical position and add the offset to it
setY(elem, posY(elem) + pos);
}
知道如何获取元素位置之后,我们再来看看如何获取元素的尺寸,
获取元素当前的高度和宽度
复制代码 代码如下:
function getHeight(elem) {
return parseInt(getStyle(elem, 'height'));
}
function getWidth(elem) {
return parseInt(getStyle(elem, 'width'));
}
大多数情况下,以上的方法够用了,但是在一些动画交互中会出现问题。比如以0像素开始的动画,你需要事先知道元素究竟能有多高或多宽,其二当元素的display属性为none时,你会得不到值。这两个问题都会在执行动画的时候发生。为此作者给出了获得元素潜在高度和宽度的函数。
复制代码 代码如下:
//查找元素完整的、可能的高度
function fullHeight(elem) {
//如果元素是显示的,那么使用offsetHeight就能得到高度,如果没有offsetHeight,则使用getHeight()
if(getStyle(elem, 'display') != 'none')
return elem.offsetHeight || getHeight(elem);
//否则,我们必须处理display为none的元素,所以重置它的css属性以获得更精确的读数
var old = resetCSS(elem, {
display:'',
visibility:'hidden',
position:'absolute'
});
//使用clientHeigh找出元素的完整高度,如果还不生效,则使用getHeight函数
var h = elem.clientHeight || getHeight(elem);
//最后,恢复其css的原有属性
restoreCSS(elem, old);
//并返回元素的完整高度
return h;
}
//查找元素完整的、可能的宽度
function fullWidth(elem) {
//如果元素是显示的,那么使用offsetWidth就能得到宽度,如果没有offsetWidth,则使用getWidth()
if(getStyle(elem, 'display') != 'none')
return elem.offsetWidth || getWidth(elem);
//否则,我们必须处理display为none的元素,所以重置它的css以获取更精确的读数
var old = resetCSS(elem, {
display:'',
visibility:'hidden',
position:'absolute'
});
//使用clientWidth找出元素的完整高度,如果还不生效,则使用getWidth函数
var w = elem.clientWidth || getWidth(elem);
//最后,恢复原有CSS
restoreCSS(elem, old);
//返回元素的完整宽度
return w;
}
//设置一组CSS属性的函数
function resetCSS(elem, prop) {
var old = {};
//遍历每个属性
for(var i in prop) {
//记录旧的属性值
old[i] = elem.style[i];
//设置新的值
elem.style[i] = prop[i];
}
return old;
}
//恢复原有CSS属性
function restoreCSS(elem, prop) {
for(var i in prop)
elem.style[i] = prop[i];
}
还有不少内容,明天继续,写写效率低下了,笔记本屏幕太小,开个pdf,写着文章老来回切换,真是。。。是时候弄个双显了!
相关推荐:
ChatGPT错误处理与异常情况解决方法:让你的AI助手更加智能与可靠,智能ai鼠标ai智能语音
ChatGPT美区要梯子吗?轻松畅享全球智能聊天体验,ai音位
AI自动读文:让阅读更轻松、更高效的智能革命,寄ai空
智能AI生成文章释放创作新可能
SEO收集:如何通过有效数据收集提升网站排名,鞍山商城网站建设报价
SEO培训:助力企业实现互联网营销的无限可能,平塘网站优化推广价格
seo词库优化,搜索词条优化 ,ai点选择
SEO词的魔力:如何通过关键词优化打破流量瓶颈,免费网站建设高端
ChatGPT打不开实时问题解决方案:让你的AI助手始终在线,psd 转ai
SEO广告:如何借助SEO提升品牌曝光与销售业绩?,网站推广怎么选择
SEO中权重是什么意思?让你迅速网站排名的核心秘密!,长颈鹿智能AI点读机
揭开“好的AI软件”背后的秘密:让生活和工作更智能的利器
SEO与SEM:谁才是提升网站流量的王者?,Ai中字体如何变形立体
ChatGPT可以实现新闻报道的即时自动化生成,怎么更改ai2的图标
SEO针对中小企业的增长潜力:如何通过精准优化抢占市场先机,网络推广和营销就选y火10星评价
seo要学什么语言,做seo需要什么语言 ,ai martino
SEO优化工具优势:提升网站排名,带来流量和转化的秘密武器,ai圆形造字
SEO与网络推广机构:如何选择最适合你的数字营销合作伙伴,ai写作软件性价比高吗
为什么说seo重要,为什么说seo重要一点 ,中通智能ai是什么意思
SEO需要什么语音,seo需要考虑什么 ,学生作业ai
企业关键字-助力企业成功的隐形动力,闽侯县企业网站建设
为什么要seo 运营,为什么需要seo ,ai人物头盔
GPT怎么收费?揭秘AI技术的定价与价值,ai报考高考
OpenAI新产品与现有技术的完美结合:赋能未来的智能变革,培训 ai
seo该从什么开始,seo是什么时候开始的 ,ai写作免费英文怎么说
SEO但是,这些常见误区你真的知道吗?,凤岗网站建设开发
SEO优化知识全解析:提升网站排名的秘密武器,ai出错合集
AI智能软件:未来科技的核心力量
SEO提供:如何通过精准的SEO策略提升网站流量与品牌影响力,刷关键词排名立的火星
SEO和品牌营销:如何通过搜索引擎优化打造品牌影响力,建邺seo软件
SEO运营工作是什么,seo公司运营 ,720516AI
用AI生成文章,让创作更简单高效
ChatGPTO1免费:突破智能聊天的极限,体验AI无限可能,糯米ai唱歌
ChatGPT的诞生,预示着人工智能大规模应用的时代已经来临,ai此生不渝
SEO站群:打造强大网络营销引擎,助力企业快速提升排名与流量,seo网站排名案例
乘风SEO:突破困境,领航网络营销新时代,武汉靠谱的关键词排名
SEO关键词是什么意思?全面解析SEO关键字的核心作用,华为ai音箱 百度ai音箱
ChatGPT一经发布,便受到了用户的狂热追捧,引爆人工智能热潮,十代ai达人办公本
SEO外包:提升网站排名与品牌曝光的最佳选择,大创建设网站
SEO未来:如何在变革中抢占先机,迎接数字营销的新纪元,台州网站建设分析和总结
GPT人工智能-让未来触手可及的智慧之光,ai头花写真
SEO舆情:如何通过有效的舆情管理提升企业品牌形象,seo推广外包提高收录
ChatGPT-4中文免费破解版:无需付费,体验最强AI助手,ai写作生成器官方下载
SEO可以改变你的商业未来:如何通过优化提升网站流量和转化率,五粮液营销推广
SEO在线服务-让您的网站快速跃升至搜索引擎前列,江西小红书营销推广案例
从语言助手到智能生活伙伴,未来的智能助手如何改变我们的生活,网站建设分站公司
SEO客服:如何提升客户体验与业务转化的双赢策略,鄂州网站建设公司教程
ChatGPT出现报错503?这些解决办法你必须知道!,粉墨ai说唱
实用AI工具:提升效率、优化生活的科技利器
企业营销:如何在竞争激烈的市场中脱颖而出,美装网站Seo优化