Vue 修改行内样式 + prefixStyle 样式兼容处理

效果

通过行内样式控制图片的缩放,旋转。

应用:图像预览组件

<template>

  <div class="img-container">

    <div v-if="imageUrl" ref="imgPreview" :class="{'image-preview':true,'image-preview-scale':isScale}">

      <div class="image-wrapper">

        <img ref="img" :src="imageUrl">

      </div>

      <div class="image-action">

        <el-button icon="el-icon-search" circle @click="handleScale" />

        <el-button icon="el-icon-refresh-left" circle @click="handleRotateLeft" />

        <el-button icon="el-icon-refresh-right" circle @click="handleRotateRight" />

        <el-button icon="el-icon-refresh" circle @click="handleReset" />

      </div>

    </div>

    <div v-else><p>相关图片缺失</p></div>

  </div>

</template>

 

<script>

import { prefixStyle } from '@/utils/prefixStyle'

const transformStyle = prefixStyle('transform')

// console.log(transformStyle) //webkitTransform

 

export default {

  name: 'ImgView',

  props: {

    fileUrl: {

      type: String,

      default: ''

    }

  },

  data() {

    return {

      isScale: false,

      loading: false,

      imageUrl: '',

      rotate: 0,

      scale: 1

    }

  },

  watch: {

    fileUrl() {

      this.imageUrl = this.fileUrl

    }

  },

  mounted() {

    this.imageUrl = this.fileUrl

  },

  methods: {

    handleScale() {

      // 图像放大

      this.isScale = true

      this.scale = this.scale + 0.5

      // transform兼容处理

      // style html内嵌入样式

      // 页面审查元素 未见 添加的前缀 但 测试了火狐 Opera 浏览器 可用

      this.$refs.imgPreview.style[transformStyle] = 'scale(' + this.scale + ')'

    },

    handleRotateLeft() {

      // 图像旋转

      this.rotate = (this.rotate - 90) % 360

      this.$refs.img.style[transformStyle] = 'rotate(' + this.rotate + 'deg)'

    },

    handleRotateRight() {

      // 图像旋转

      this.rotate = (this.rotate + 90) % 360

      this.$refs.img.style[transformStyle] = 'rotate(' + this.rotate + 'deg)'

    },

    handleReset() {

      // 图像重置

      this.rotate = 0

      this.scale = 1

      this.isScale = false

      this.$refs.img.style[transformStyle] = 'rotate(0deg)'

      this.$refs.imgPreview.style[transformStyle] = 'scale(1)'

    }

  }

}

</script>

 

<style lang="scss" scoped>

div.img-container{

  width: 100%;

  height:100%;

  background: #f7f7f7;

  .image-preview {

    box-sizing: border-box;

    padding:10px;

    .image-wrapper {

        box-sizing: content-box;

        text-align: center;

        img {

            width: 100%;

            height: auto;

        }

    }

    .image-action {

      text-align: right;

    }

  }

  .image-preview-scale{

    position: absolute;

    top:0;

    left:0;

    background: #f7f7f7;

    z-index: 2000;

  }

}

</style>

 

样式兼容处理:prefixStyle.js

/* jshint esversion: 6 */

export function prefixStyle(style) {

  // 创建一个元素来判断当前浏览器使用的前缀

  const elementStyle = document.createElement('div').style

  // elementStyle 返回的是 CSSStyleDeclaration 对象

  // console.log(elementStyle)

 

  const prefix = (() => {

    const transformNames = {

      webkit: 'webkitTransform',

      Moz: 'MozTransform',

      O: 'OTransform',

      ms: 'msTransform',

      standard: 'transform'

    }

 

    for (const key in transformNames) {

      if (elementStyle[transformNames[key]] !== undefined) {

        return key

      }

    }

 

    return false

  })()

 

  if (prefix === false) {

    return false

  }

  if (prefix === 'standard') {

    return style

  }

  // 拼接:前缀+样式首字母大写+样式剩余字符

  return prefix + style.charAt(0).toUpperCase() + style.substr(1)

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Vue 3是一种流行的JavaScript框架,它提供了一种简洁、高效的方式来构建用户界面。Vite是一个基于ES模块的构建工具,它专注于快速的开发体验。而TypeScript是一种静态类型检查的JavaScript超集,它可以增强代码的可读性和可维护性。 在浏览器兼容性方面,Vue 3、Vite和TypeScript都有一些要注意的事项: 1. Vue 3:Vue 3相对于Vue 2有一些重要的变化,其中一个是使用了ES模块作为默认的模块系统。这意味着在一些旧版本的浏览器中可能不被支持。为了解决这个问题,你可以使用Babel等工具进行转译,以确保Vue 3代码在目标浏览器中能够正常运行。 2. Vite:Vite使用ES模块作为默认的模块系统,并且利用现代浏览器的原生ES模块支持来提供快速的开发体验。然而,对于一些旧版本的浏览器,可能需要进行转译和polyfill处理。你可以使用Babel和相关插件来处理这些兼容性问题。 3. TypeScript:TypeScript可以编译为普通的JavaScript代码,并且可以在大多数现代浏览器中运行。然而,一些较旧的浏览器可能不支持所有的ES6+特性,因此你可能需要使用Babel等工具来进行转译和polyfill处理。 总结起来,为了在Vue 3、Vite和TypeScript项目中实现浏览器兼容性,你可以采取以下步骤: 1. 使用Babel:配置Babel来转译和处理目标浏览器不支持的特性。 2. 使用Polyfill:使用Polyfill来填充目标浏览器缺少的功能。 3. 针对不同浏览器版本进行测试:确保你的应用在目标浏览器中能够正常运行,并进行必要的调整和修复。 4. 参考官方文档:查阅Vue 3、Vite和TypeScript的官方文档,了解更多关于浏览器兼容性的建议和最佳实践。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Irene1991

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

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

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

打赏作者

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

抵扣说明:

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

余额充值