当前位置: 首页 > Web前端 > CSS

css常用操作

时间:2023-03-31 13:39:41 CSS

获取元素宽高1.只能获取内联样式varele=document.getElementById('element');console.log(ele.style.width);//空字符串console.log(ele.style.height);//'100px'2.可以获取实时styleMDN数据varele=document.getElementById('element');console.log(window.getComputedStyle(ele).width);//'100px'console.log(window.getComputedStyle(ele).height);//'100px'3、Element.currentStyle.width/height作用同第二点,只存在于老版本IE(IE9以下),除了老版本IE兼容,所以不要使用它。4.除了获取宽高之外,还可以获取元素MDN数据的位置等信息varele=document.getElementById('element');console.log(ele.getBoundingClientRect().width);//100console.log(ele.getBoundingClientRect().height);//Round1001.Round1.//舍去小数部分,保留整数部分2.parseInt(5/2)  //22.Roundup1.//Up四舍五入,如果有小数,整数部分加12。Math.ceil(5/2)  //33.向下取整1.//向下取整,舍去小数部分2.Math.floor(5/2)  //24取整1.//四舍五入2.Math.round(5/2)  //3余数1.//余数2.6%4  //2