js复习:移动元素offsetLeft,offsetTop,10%,30%, left

1.我们怎么移动元素?

1.给元素绝对定位,或相对定位 ,
position: relative; 有left和right.相对于

offsetLeft值:元素加了定位,看上一级的定位元素有关,是与
上一级的定位元素的偏移量。
如果:上一级定位元素是body,就是元素相对于body偏移量

在js中:有
div.style.left=div.offsetLeft+50+‘px’;//可以控制元素位置

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>



<style>


    div{
position: absolute;
left: 30px;
        width: 300px;height: 300px;
        background-color: pink;
       
    }
</style>
<body>
    



<div>




</div>

<button>1111</button>

<script>



var div=document.querySelector("div");
var button=document.querySelector("button");
console.log(div.offsetLeft);




button.οnclick=function(){

div.style.left=div.offsetLeft+50+'px';

console.log("我是");

}


</script>




</body>
</html>

在这里插入图片描述

点击:按钮元素移动了:offseLf:变化中
可以通过:div.style.left=div.offsetLeft+50+‘px’;
实现:元素移动
在这里插入图片描述

加一个li:让它相对于div偏移:加定位

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>



<style>


    div{
position: relative;/*加了定位,offsetLeft是相对于body偏移量*/
left: 30px;
        width: 300px;height: 300px;
        background-color: pink;
       
    }
    li{
position: relative;/*加了定位*/
width: 10px;
height: 10px;
background-color: brown;

    }
</style>
<body>
    



<div>


    <li>11111111111</li>


</div>

<button>1111</button>

<script>



var div=document.querySelector("div");
var button=document.querySelector("button");
var li=document.querySelector("li");
console.log(div.offsetLeft);




button.οnclick=function(){

div.style.left=div.offsetLeft+50+'px';
li.style.left=li.offsetLeft+10+'px';

console.log(div.offsetLeft);
console.log("li的偏移量");
console.log(li.offsetLeft);

}


</script>




</body>
</html>

效果图:

在这里插入图片描述

这个可以应用:轮播图

第二种方法:移动元素

var index=0
div.style.left=(index*(100))+’%’;
index++;

父元素的变化,给left赋值为父元素的10%,100%
*li.style.left=(index10)+’%’;/ 相对于 /

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>



<style>


    div{
position: relative;/*加了定位,offsetLeft是相对于body偏移量*/
left: 30px;
        width: 300px;height: 300px;
        background-color: pink;
       
    }
    li{
position: relative;
width: 10px;
height: 10px;
background-color: brown;

    }
</style>
<body>
    



<div>


    <li>11111111111</li>


</div>

<button>1111</button>

<script>



var div=document.querySelector("div");
var button=document.querySelector("button");
var li=document.querySelector("li");
index=0;
console.log(div.offsetLeft);




button.οnclick=function(){


li.style.left=(index*10)+'%';/* 相对于 */

console.log(li.style.left);
console.log("li的偏移量");
console.log(li.offsetLeft);
index++;

}


</script>




</body>
</html>

效果图:

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半浮名

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值