Home Web Front-end HTML Tutorial CSS之Transform_html/css_WEB-ITnose

CSS之Transform_html/css_WEB-ITnose

Jun 21, 2016 am 08:47 AM

我们利用css3中的transform可以实现文字或图像的旋转、缩放、倾斜和移动四种类型的变形处理。

本文使用的是标准的transform,亦可以使用各个浏览器对应私有的属性。对应如下

  • -webkit-transform
  • -moz-transform
  • -o-transform

不过在我的电脑上用transform效果相同,Chrome 49  Firefox 46 IE 10

一、旋转(rotate)

<!doctype html><html lang="en"><head>    <meta charset="UTF-8">    <title>Document</title>    <style>        img:hover{            transform: rotate(90deg);        }    </style></head><body>    <img  src="/static/imghw/default1.png"  data-src="http://dl.bizhi.sogou.com/images/2012/03/23/107607.jpg?f=download"  class="lazy" alt="CSS之Transform_html/css_WEB-ITnose" ></body></html>
Copy after login

语法格式如下:

rotate(angle)

旋转角度单位有:

  • deg 指角度制,一周的角度是360°,直角是90°
  • grad 指百分度制,一周的角度是400,直角是100
  • turn 圈, 1turn代表旋转一圈
  • rad 指弧度制,一周的角度是2π,直角是π/2

转换比例为: 90deg = 100grad = 0.25turn ≈ 1.570796326794897rad

二、缩放(scale)

<!doctype html><html lang="en"><head>    <meta charset="UTF-8">    <title>Document</title>    <style>        img:hover{            transform: scale(1,-1)        }    </style></head><body>    <img  src="/static/imghw/default1.png"  data-src="http://dl.bizhi.sogou.com/images/2012/03/23/107607.jpg?f=download"  class="lazy" alt="CSS之Transform_html/css_WEB-ITnose" ></body></html>
Copy after login

语法格式如下:

scale(number,[number2])

number2是可选的,如果未设置,则与默认与number相同。number可以为正数、负数、小数。

我们将number分为两部分,正负号(+,-)和数值(number的绝对值)。负号(-)意味着元素要被旋转180度,数值部分大于1则放大,小于一则缩小,等于一既不放大又不缩小,为零时消失。

三、移动(translate)

<!doctype html><html lang="en"><head>    <meta charset="UTF-8">    <title>Document</title>    <style>        img:hover{            transform: translate(2in,5cm)        }    </style></head><body>    <img  src="/static/imghw/default1.png"  data-src="http://dl.bizhi.sogou.com/images/2012/03/23/107607.jpg?f=download"  class="lazy" alt="CSS之Transform_html/css_WEB-ITnose" ></body></html>
Copy after login

语法格式如下:

translate(value1,[value2])

第一个参数表示相对于原位置X轴偏移距离,第二个参数表示相对于Y轴偏移个距离,如果省略了第二个参数,则默认为0。正数向右和下偏移,负数向左和上偏移。

距离的单位有:

  • 绝对长度单位
    • px
    • in
    • cm
    • mm
  • 相对字体长度单位
    • em
    • rem
    • pt
    • pc
    • ex
    • ch
  • 可视区百分比长度单位
    • vw
    • vh
    • vmin
    • vmax
  • 离奇的长度单位
    • %(百分比)

关于这些长度单位,这有一篇很好的介绍链接http://www.w3cplus.com/css/the-lengths-of-css.html,本段也是参照此链接,不过提示大家,浏览器的版本也在不断的迭代,有些特性需要自己去验证,比如vmax属性在我的chrome浏览器下也行正常运行。

四、倾斜(skew)

<!doctype html><html lang="en"><head>    <meta charset="UTF-8">    <title>Document</title>    <style>        img:hover{            transform: skew(30deg,-0.1turn)        }    </style></head><body>    <img  src="/static/imghw/default1.png"  data-src="http://dl.bizhi.sogou.com/images/2012/03/23/107607.jpg?f=download"  class="lazy" alt="CSS之Transform_html/css_WEB-ITnose" ></body></html>
Copy after login

语法格式:

skew(angle,[angle])

第一个参数表示相对于X轴进行倾斜,第二个参数相对于Y轴进行倾斜,如果省略了第二个参数,则默认为0。

rotate函数只是旋转,不改变元素的形状,但skew函数会改变元素的形状。

五、变形

<!doctype html><html lang="en"><head>    <meta charset="UTF-8">    <title>Document</title>    <style>        img:hover{            transform: matrix(1,0.4,0,1,0,0)        }    </style></head><body>    <img  src="/static/imghw/default1.png"  data-src="http://dl.bizhi.sogou.com/images/2012/03/23/107607.jpg?f=download"  class="lazy" alt="CSS之Transform_html/css_WEB-ITnose" ></body></html>
Copy after login

matrix()函数是一个3x3的矩阵,将旧的参数转换成新的参数。

目前,我对此也不是很了解,大家如有兴趣自行Google。

六、Transform-origin

CSS变形的原点默认是对象的中心点,如果要改变这个中心点,可以使用transform-origin属性进行定义。

<!doctype html><html lang="en"><head>    <meta charset="UTF-8">    <title>Document</title>    <style>        img:hover{            /*以左上角为原点旋转*/            transform-origin: top left;            transform: rotate(30deg);        }    </style></head><body>    <img  src="/static/imghw/default1.png"  data-src="http://dl.bizhi.sogou.com/images/2012/03/23/107607.jpg?f=download"  class="lazy" alt="CSS之Transform_html/css_WEB-ITnose" ></body></html>
Copy after login

语法格式:

    transform-origin:[ [ | | left | center | right ][ | | top | center | buttom ] ? ] | [ [ left | center | right ] || [ top | center | bottom ]]

transform-origin属性的初始值是50%,50%,使用与块状元素和内联元素。transform-origin接受连个参数,他们可以是百分比、em、px等具体值也可以是left、center、right,或者top、middle、bottom等描述性关键字。

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

How to implement adaptive layout of Y-axis position in web annotation? How to implement adaptive layout of Y-axis position in web annotation? Apr 04, 2025 pm 11:30 PM

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

See all articles