JavaScript--currentStyle和getComputedStyle()的区别和联系

JavaScript中的currentStyle和getComputedStyle()都是用于获取元素的计算样式属性的方法,但它们适用于不同的情况。下面会对它们进行解释:

1.currentStyle:该属性是IE浏览器特有的,并且仅能在IE浏览器中使用。它用于获取指定元素的当前样式,返回的是一个样式属性对象。
示例用法: 

const element = document.getElementById('myElement');
const currentStyle = element.currentStyle;

// 获取背景颜色
const backgroundColor = currentStyle.backgroundColor;
console.log(backgroundColor);

2.getComputedStyle():该方法可以获取指定元素的计算样式,适用于大多数现代浏览器(包括Chrome、Firefox、Safari等)。它返回的也是一个样式属性对象。 
示例用法: 

const element = document.getElementById('myElement');
const computedStyle = window.getComputedStyle(element);

// 获取背景颜色
const backgroundColor = computedStyle.backgroundColor;
console.log(backgroundColor);

 需要注意的是,无论是currentStyle还是getComputedStyle()返回的样式值都是字符串形式,如果需要获取具体的数值,可以使用相关的方法进行转换(例如parseInt())。

因此,我们可以写一个适配各个浏览器的读取元素样式的方法。

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>适配浏览器的元素样式读取方法</title>
  <style>
    .example {
      width: 200px;
      height: 100px;
      background-color: red;
      color: white;
      font-size: 20px;
      padding: 10px;
      margin: 20px;
    }
  </style>
  <script>
    // 元素样式读取方法
    function getElementStyle(element, styleProperty) {
      if (element.currentStyle) { // IE浏览器
        return element.currentStyle[styleProperty];
      } else if (window.getComputedStyle) { // 标准浏览器
        return window.getComputedStyle(element)[styleProperty];
      }
    }

    // 示例用法
    window.onload = function() {
      var exampleElement = document.getElementById('example');
      var width = getElementStyle(exampleElement, 'width');
      var fontSize = getElementStyle(exampleElement, 'fontSize');

      console.log("宽度: " + width);
      console.log("字体大小: " + fontSize);
    };
  </script>
</head>
<body>
  <div id="example" class="example">示例元素</div>
</body>
</html>

在这个示例中,我们定义了一个名为getElementStyle的函数。该函数接受两个参数:element代表要获取样式的元素,styleProperty代表要获取的样式属性。
getElementStyle函数首先判断浏览器是否支持currentStyle属性,如果支持,则使用element.currentStyle[styleProperty]来获取元素的样式值。
如果浏览器不支持currentStyle属性,则判断是否支持getComputedStyle方法,如果支持,则使用window.getComputedStyle(element)[styleProperty]来获取元素的样式值。
在示例代码中,我们使用了getElementStyle函数来获取示例元素的宽度和字体大小,并将结果打印到控制台上。你可以根据需要修改和扩展这个示例来适应不同的浏览器和样式属性。

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
el-dialog是Element UI中的一个组件,用于创建对话框。要实现el-dialog的拖拽和拉伸功能,可以使用自定义指令el-drag-dialog。 以下是一个示例代码,演示了如何使用el-dialog和el-drag-dialog实现拖拽和拉伸功能: ```html <template> <div> <el-dialog v-el-drag-dialog :visible.sync="dialogVisible" title="对话框" :width="dialogWidth"> <p>这是一个对话框</p> </el-dialog> </div> </template> <script> export default { data() { return { dialogVisible: false, dialogWidth: '50%' }; }, directives: { 'el-drag-dialog': { bind(el, binding, vnode) { el.style.cursor = 'move'; el.onmousedown = (e) => { const dialogHeaderEl = el.querySelector('.el-dialog__header'); const dragDom = vnode.componentInstance.$el.querySelector('.el-dialog'); const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null); const disX = e.clientX - dialogHeaderEl.offsetLeft; const disY = e.clientY - dialogHeaderEl.offsetTop; const screenWidth = document.body.clientWidth; const screenHeight = document.documentElement.clientHeight; const dragDomWidth = dragDom.offsetWidth; const dragDomheight = dragDom.offsetHeight; const minDragDomLeft = dragDom.offsetLeft; const maxDragDomLeft = screenWidth - dragDom.offsetLeft - dragDomWidth; const minDragDomTop = dragDom.offsetTop; const maxDragDomTop = screenHeight - dragDom.offsetTop - dragDomheight; let styL = sty.left; let styT = sty.top; document.onmousemove = function (e) { let left = e.clientX - disX; let top = e.clientY - disY; if (-(left) > minDragDomLeft) { left = -(minDragDomLeft); } else if (left > maxDragDomLeft) { left = maxDragDomLeft; } if (-(top) > minDragDomTop) { top = -(minDragDomTop); } else if (top > maxDragDomTop) { top = maxDragDomTop; } dragDom.style.left = `${left + parseInt(styL, 10)}px`; dragDom.style.top = `${top + parseInt(styT, 10)}px`; }; document.onmouseup = function () { document.onmousemove = null; document.onmouseup = null; }; }; } } } }; </script> ``` 在上述代码中,我们使用了自定义指令el-drag-dialog来实现拖拽功能。通过绑定该指令到el-dialog的元素上,我们可以通过鼠标按下、移动和释放的事件来实现对话框的拖拽。 同时,我们还可以通过设置dialogWidth的值来控制对话框的宽度,从而实现拉伸功能。 请注意,上述代码是一个Vue组件,需要在Vue项目中使用。确保已经引入了Element UI库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

正在奋斗的程序猿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值