亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

offsetWidth前添加負號有什么意義呢?我去掉這個負號時我的代碼就出錯

offsetWidth前添加負號有什么意義呢?我去掉這個負號時我的代碼就出錯

qq_小柯_1 2015-11-28 15:27:28
<!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" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>Document</title><style type="text/css">body,img,a,div,ul,li{margin: 0px;padding: 0px;}? ? ? ? ? #div1{? ? ? ? ? ?width: 600px;? ? ? ? ? ?height: 100px;? ? ? ? ? ?background: red;? ? ? ? ? ?position: relative;? ? ? ? ? ?overflow: hidden;? ? ? ? ? ?margin:0px auto;? ? ? ? ? ?margin-top: 100px;? ? ? ? ? }? ? ? ? ? #div1 ul{? ? ? ? ? ?position: absolute;? ? ? ? ? ?left: 0;? ? ? ? ? ?top: 0;? ? ? ? ? }? ? ? ? ? #div1 ul li{? ? ? ? ? ?list-style: none;? ? ? ? ? ?float: left;? ? ? ? ? ?width: 150px;? ? ? ? ? ?height: 100px;? ? ? ? ? }? ? ? ? ? #div1 ul li img{? ? ? ? ? ?width: 150px;? ? ? ? ? ?height: 100px;? ? ? ? ? }? ? ? ? ? a{? ? ? ? ??? ? ? ? ? ?display:block;? ? ? ? ? ?width: 50px;? ? ? ? ? ?height: 50px;? ? ? ? ? ?background: orange;? ? ? ? ? ?float: left;? ? ? ? ? ?margin:;? ? ? ? ? ?? ? ? ? ? }</style><script type="text/javascript">? ? ? ? ?window.onload=function(){? ? ? ? ? var oDiv=document.getElementById('div1');? ? ? ? ? var oUl=document.getElementById('ul1');? ? ? ? ? var ali=document.getElementsByTagName('li');? ? ? ? ? oUl.innerHTML+=oUl.innerHTML;? ? ? ? ? oUl.style.width=ali[0].offsetWidth*ali.length+'px';? ? ? ? ? ? var time=null;? ? ? ? ? ? var speed=2;? ? ? ? ? ?? ? ?? ? ? ? ? ? function move(){? ? ? ? ? ? ?if(oUl.offsetLeft<-oUl.offsetWidth/2){? ? ? ? ? ? ?oUl.style.left='0';? ? ? ? ? ? ?? ? ? ? ? ? ?}? ? ? ? ? ? ?if(oUl.offsetLeft>0){? ? ? ? ? ? ?oUl.style.left=-oUl.offsetWidth/2+'px';? ? ? ? ?? ? ? ? ? ? ?}? ? ? ? ? ? ?oUl.style.left=oUl.offsetLeft+speed+'px';? ? ? ? ? ? }? ? ? ? ? ? time=setInterval(move,30);? ? ? ? ? ??? ? ? ? ? ? oDiv.onmousemove=function(){? ? ? ? ? ? ?clearInterval(time);? ? ? ? ? ? };? ? ? ? ? ? oDiv.onmouseout=function(){? ? ? ? ? ? ? ? ?time=setInterval(move,30);? ? ? ? ? ? };? ? ? ? ? ? document.getElementsByTagName('a')[0].onclick=function(){? ? ? ? ? ? ?speed=-2;? ? ? ? ? ? };? ? ? ? ? ? ?document.getElementsByTagName('a')[1].onclick=function(){? ? ? ? ? ? ?speed=2;? ? ? ? ? ? };? ? ? ? ? ?? ? ? ? ?};</script></head><body><div id="div1"><ul id="ul1"><li ><img src="img/0.jpg"></li><li><img src="img/1.jpg"></li><li><img src="img/2.jpg"></li><li><img src="img/3.jpg"></li></ul></div><a href="javascript:;">向左走</a><a href="javascript:;">向右走</a></body></html>
查看完整描述

2 回答

?
echo_kinchao

TA貢獻600條經驗 獲得超86個贊

因為是要算定位啊

查看完整回答
反對 回復 2015-11-28
  • qq_小柯_1
    qq_小柯_1
    啊?可以為什么算定位要添加負號呀?我還是不怎么懂使用它的這個思想
?
慕男嬸

TA貢獻98條經驗 獲得超73個贊

因為?oUl.offsetLeft 得到的結果可能是負值。

所以,在offsetWidth前面加了一個符號來比較大小。

注意:offsetWidth只能是正值。因為取的是寬度嘛,寬度哪兒有負值?

如果不加的話,

正數是永遠大于負數的

查看完整回答
反對 回復 2015-11-28
  • 2 回答
  • 0 關注
  • 3400 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號