老师,请问3-7如果用position怎么达到同样效果?
老师在讲课的时候只说了offset()和position()的获取方法,并没有讲如何设置?请老师说明一下?
正在回答 回答被采纳积分+1
1、offset()方法的定义和用法:此方法返回或设置所匹配元素相对于document对象的偏移量。语法结构一:$(selector).offset()获取匹配元素在当前document的相对偏移。返回的对象包含两个整型属:top和left。此方法只对可见元素有效。
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<title>offset()</title>
<style type="text/css">
*{
margin:0px;
padding:0px;
}
.father{
border:1px solid black;
width:400px;
height:300px;
padding:10px;
margin:50px;
}
.children{
height:150px;
width:200px;
margin-left:50px;
background-color:green;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
a=$(".children").offset();
alert("元素的偏移量坐标是:"+a.top+"|"+a.left+"");
})
})
</script>
</head>
<body>
<div class="father">
<div class="children"></div>
</div>
<button>获取元素的坐标</button>
</body>
</html>语法结构二:$(selector).offset(value)设置匹配元素相对于document对象的坐标。offset()方法可以让我们重新设置元素的位置。这个元素的位置是相对于document对象的。如果对象原先的position样式属性是static的话,会被改成relative来实现重定位。其中value规定以像素计的 top 和 left 坐标。可能的值:1.值对,比如 {top:200,left:10}。2.带有top和left 属性的对象。
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<title>offset()</title>
<style type="text/css">
.father{
border:1px solid black;
width:400px;
height:300px;
}
.children{
height:150px;
width:200px;
background-color:green;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$(".children").offset({top:100,left:100})
})
})
</script>
</head>
<body>
<div class="father">
<div class="children"></div>
</div>
<button>点击设置偏移量</button>
</body>
</html>以上代码可以设置div相对于document的偏移量。
语法结构三:$(selector).offset(function(index,oldoffset))。function(index,oldvalue)规定返回被选元素新偏移坐标的函数:index - 可选。元素的索引。oldvalue - 可选。当前坐标。
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<title>offset()</title>
<style type="text/css">
.father{
border:1px solid black;
width:400px;
height:300px;
}
.children{
height:150px;
width:200px;
background-color:green;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$(".children").offset(function(a,b){
var newpoint= new Object();
newpoint.top=b.top+50;
newpoint.left=b.left+50;
return newpoint;
})
})
})
</script>
</head>
<body>
<div class="father">
<div class="children"></div>
</div>
<button>点击设置偏移量</button>
</body>
</html>position()方法的定义和用法:此方法获取匹配元素相对某些元素的偏移量。返回一个包含两个整型属性(top和left)的对象。此方法只对可见元素有效。特别说明:偏移是从匹配元素的margin外边距开始计算的。语法:$(selector).position()
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<title>position()</title>
<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 src="http://libs.baidu.com/jquery/1.9.0/jquery.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>希望可以帮到你~
- 参与学习 人
- 提交作业 11218 份
- 解答问题 36712 个
从一个不会编程的小白到一个老司机是需要过程的,首先得入门,学习基础知识,然后才能进阶,最后再到精通,本专题是你走进前端世界的不二选择!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星