整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

html页面中css缩放图片的方法

html页面制作中,可以利用 CSS 中的 transform 属性对图片进行旋转,缩放,移动或倾斜的操作。而今天我们只说说 transform 属性对图片进行等比例的缩放操作。

css transform 属性的介绍

transform:该属性向元素应用 2D 或 3D 转换。它允许我们对元素进行旋转、缩放、移动或倾斜的操作。

缩放使用值:

scaleY(n):对高度进行缩n倍的缩放

scaleX(n):对宽度进行缩放,n指的是缩放比例

scale(n):对整体高度和宽度进行缩放,n为缩放的比例,为数字

示例代码:

transform: scale(2)

css图片宽度缩放

scaleX():可以对元素(图片)的宽度进行缩放,以下代码是将原来的图片的宽度放大了2倍。

示列代码:

<div class="divimg">
 <img src="biao.png" >
 <br/>
 <!--宽度缩放-->
 <img src="biao.png" style="transform:scaleX(2);">
</div>

运行结果:

用鼠标改变元素的尺寸。

如需了解更多有关 resizable 交互的细节,请查看 API 文档 可调整尺寸小部件(Resizable Widget)。

默认功能

在任意的 DOM 元素上启用 resizable 功能。通过鼠标拖拽右边或底边的边框到所需的宽度或高度。

<!doctype html><html lang="en"><head>
 <meta charset="utf-8">
 <title>jQuery UI 缩放(Resizable) - 默认功能</title>
 <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
 <script src="//code.jquery.com/jquery-1.9.1.js"></script>
 <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
 <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
 <style>
 #resizable { width: 150px; height: 150px; padding: 0.5em; }
 #resizable h3 { text-align: center; margin: 0; }
 </style>
 <script>
 $(function() {
 $( "#resizable" ).resizable();
 });
 </script></head><body>
<div id="resizable" class="ui-widget-content">
 <h3 class="ui-widget-header">缩放(Resizable)</h3></div>
</body></html>

查看演示

动画

使用 animate 选项(布尔值)使缩放行为动画化。当该选项设置为 true 时,拖拽轮廓到所需的位置,元素会在拖拽停止时以动画形式调整到该尺寸。

<!doctype html><html lang="en"><head>
 <meta charset="utf-8">
 <title>jQuery UI 缩放(Resizable) - 动画</title>
 <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
 <script src="//code.jquery.com/jquery-1.9.1.js"></script>
 <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
 <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
 <style>
 #resizable { width: 150px; height: 150px; padding: 0.5em; }
 #resizable h3 { text-align: center; margin: 0; }
 .ui-resizable-helper { border: 1px dotted gray; }
 </style>
 <script>
 $(function() {
 $( "#resizable" ).resizable({
 animate: true
 });
 });
 </script></head><body>
<div id="resizable" class="ui-widget-content">
 <h3 class="ui-widget-header">动画</h3></div>
</body></html>

查看演示

限制缩放区域

定义缩放区域的边界。使用 containment 选项来指定一个父级的 DOM 元素或一个 jQuery 选择器,比如 'document.'。

<!doctype html><html lang="en"><head>
 <meta charset="utf-8">
 <title>jQuery UI 缩放(Resizable) - 限制缩放区域</title>
 <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
 <script src="//code.jquery.com/jquery-1.9.1.js"></script>
 <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
 <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
 <style>
 #container { width: 300px; height: 300px; }
 #container h3 { text-align: center; margin: 0; margin-bottom: 10px; }
 #resizable { background-position: top left; width: 150px; height: 150px; }
 #resizable, #container { padding: 0.5em; }
 </style>
 <script>
 $(function() {
 $( "#resizable" ).resizable({
 containment: "#container"
 });
 });
 </script></head><body>
<div id="container" class="ui-widget-content">
 <h3 class="ui-widget-header">限制</h3>
 <div id="resizable" class="ui-state-active">
 <h3 class="ui-widget-header">缩放(Resizable)</h3>
 </div></div>
</body></html>

查看演示

延迟开始

通过 delay 选项设置延迟开始缩放的毫秒数。通过 distance 选项设置光标被按下且拖拽指定像素后才允许缩放。

<!doctype html><html lang="en"><head>
 <meta charset="utf-8">
 <title>jQuery UI 缩放(Resizable) - 延迟开始</title>
 <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
 <script src="//code.jquery.com/jquery-1.9.1.js"></script>
 <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
 <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
 <style>
 #resizable, #resizable2 { width: 150px; height: 150px; padding: 0.5em; }
 #resizable h3, #resizable2 h3 { text-align: center; margin: 0; }
 </style>
 <script>
 $(function() {
 $( "#resizable" ).resizable({
 delay: 1000
 });
 $( "#resizable2" ).resizable({
 distance: 40
 });
 });
 </script></head><body>
<h3 class="docs">时间延迟 (ms):</h3><div id="resizable" class="ui-widget-content">
 <h3 class="ui-widget-header">时间</h3></div>
<h3 class="docs">距离延迟 (px):</h3><div id="resizable2" class="ui-widget-content">
 <h3 class="ui-widget-header">距离</h3></div>
</body></html>

查看演示

助手

通过设置 helper 选项为一个 CSS class,当缩放时只显示元素的轮廓。

<!doctype html><html lang="en"><head>
 <meta charset="utf-8">
 <title>jQuery UI 缩放(Resizable) - 助手</title>
 <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
 <script src="//code.jquery.com/jquery-1.9.1.js"></script>
 <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
 <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
 <style>
 #resizable { width: 150px; height: 150px; padding: 0.5em; }
 #resizable h3 { text-align: center; margin: 0; }
 .ui-resizable-helper { border: 2px dotted #00F; }
 </style>
 <script>
 $(function() {
 $( "#resizable" ).resizable({
 helper: "ui-resizable-helper"
 });
 });
 </script></head><body>
<div id="resizable" class="ui-widget-content">
 <h3 class="ui-widget-header">助手</h3></div>
</body></html>

查看演示

最大/最小尺寸

使用 maxHeightmaxWidthminHeightminWidth 选项限制 resizable 元素的最大或最小高度或宽度。

<!doctype html><html lang="en"><head>
 <meta charset="utf-8">
 <title>jQuery UI 缩放(Resizable) - 最大/最小尺寸</title>
 <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
 <script src="//code.jquery.com/jquery-1.9.1.js"></script>
 <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
 <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
 <style>
 #resizable { width: 200px; height: 150px; padding: 5px; }
 #resizable h3 { text-align: center; margin: 0; }
 </style>
 <script>
 $(function() {
 $( "#resizable" ).resizable({
 maxHeight: 250,
 maxWidth: 350,
 minHeight: 150,
 minWidth: 200
 });
 });
 </script></head><body>
<div id="resizable" class="ui-widget-content">
 <h3 class="ui-widget-header">放大/缩小</h3></div>
</body></html>

查看演示

保持纵横比

保持现有的纵横比或设置一个新的纵横比来限制缩放比例。设置 aspectRatio 选项为 true,且可选地传递一个新的比率(比如,4/3)。

<!doctype html><html lang="en"><head>
 <meta charset="utf-8">
 <title>jQuery UI 缩放(Resizable) - 保持纵横比</title>
 <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
 <script src="//code.jquery.com/jquery-1.9.1.js"></script>
 <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
 <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
 <style>
 #resizable { width: 160px; height: 90px; padding: 0.5em; }
 #resizable h3 { text-align: center; margin: 0; }
 </style>
 <script>
 $(function() {
 $( "#resizable" ).resizable({
 aspectRatio: 16 / 9
 });
 });
 </script></head><body>
<div id="resizable" class="ui-widget-content">
 <h3 class="ui-widget-header">保持纵横比</h3></div>
</body></html>

查看演示

对齐到网格

对齐 resizable 元素到网格。通过 grid 选项设置网格单元的尺寸(以像素为单位的高度和宽度)。

<!doctype html><html lang="en"><head>
 <meta charset="utf-8">
 <title>jQuery UI 缩放(Resizable) - 对齐到网格</title>
 <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
 <script src="//code.jquery.com/jquery-1.9.1.js"></script>
 <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
 <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
 <style>
 #resizable { width: 150px; height: 150px; padding: 0.5em; }
 #resizable h3 { text-align: center; margin: 0; }
 </style>
 <script>
 $(function() {
 $( "#resizable" ).resizable({
 grid: 50
 });
 });
 </script></head><body>
<div id="resizable" class="ui-widget-content">
 <h3 class="ui-widget-header">网格</h3></div>
</body></html>

同步缩放

通过点击并拖拽一个元素的边来同时调整多个元素的尺寸。给 alsoResize 选项传递一个共享的选择器。

<!doctype html><html lang="en"><head>
 <meta charset="utf-8">
 <title>jQuery UI 缩放(Resizable) - 同步缩放</title>
 <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
 <script src="//code.jquery.com/jquery-1.9.1.js"></script>
 <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
 <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
 <style>
 #resizable { background-position: top left; }
 #resizable, #also { width: 150px; height: 120px; padding: 0.5em; }
 #resizable h3, #also h3 { text-align: center; margin: 0; }
 #also { margin-top: 1em; }
 </style>
 <script>
 $(function() {
 $( "#resizable" ).resizable({
 alsoResize: "#also"
 });
 $( "#also" ).resizable();
 });
 </script></head><body>
<div id="resizable" class="ui-widget-header">
 <h3 class="ui-state-active">缩放</h3></div>
<div id="also" class="ui-widget-content">
 <h3 class="ui-widget-header">同步缩放</h3></div>
</body></html>

查看演示

文本框

可缩放的文本框。

<!doctype html><html lang="en"><head>
 <meta charset="utf-8">
 <title>jQuery UI 缩放(Resizable) - 文本框</title>
 <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
 <script src="//code.jquery.com/jquery-1.9.1.js"></script>
 <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
 <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
 <style>
 .ui-resizable-se {
 bottom: 17px;
 }
 </style>
 <script>
 $(function() {
 $( "#resizable" ).resizable({
 handles: "se"
 });
 });
 </script></head><body>
<textarea id="resizable" rows="5" cols="20"></textarea>
</body></html>

视觉反馈

通过设置 ghost 选项为 true,可在缩放期间显示一个半透明的元素,而不是显示一个实际的元素。

明:SVG 虽然也是标签,但它不是 HTML5,标题加了 HTML5 只是为了与 canvas 放到一起。

一、为什么要学 SVG ?

SVG 意为可缩放矢量图形(Scalable Vector Graphics),使用 XML 格式定义矢量图形。其他的图像格式都是基于像素的,但是 SVG 没有单位的概念,它的20只是表示1的20倍,所以 SVG 绘制的图形放大或缩小都不会失真。

与其他图像比较,SVG 的优势有以下几点:

  1. SVG 可以被多个工具读取和修改。
  2. SVG 与其他格式图片相比,尺寸更小,可压缩性强。
  3. SVG 可任意伸缩。
  4. SVG 图像可以随意地高质量打印。
  5. SVG 图像可以添加文本和事件,还可搜索,适合做地图。
  6. SVG 是纯粹的 XML,不是 HTML5。
  7. SVG是W3C标准

二、SVG 形状元素

2.1、svg 标签

SVG 的代码都放到 svg 标签呢,SVG 中的标签都是闭合标签,与html中标签用法一致。svg的属性有:

  • 有width和height,指定了svg的大小。

eg:画一条直线,完整代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Document</title>
</head>
<body style="height:600px;">
 <svg width="300" height="300">
  <line x1="0" y1="0" x2="100" y2="100" stroke="black" stroke-width="20"></line>    
 </svg> 
</body>
</html>

上述 svg 设置的宽高没有带单位,此时默认是像素值,如果需要添加单位时,除了绝对单位,也可以设置相对单位。

  • viewBox 属性

使用语法:<svg viewBox=" x1,y1,width,height "></svg>

四个参数分别是左上角的横纵坐标、视口的宽高。表示只看SVG的某一部分,由上述四个参数决定。

使用 viewBox 之后,相当于svg整体大小不变,只能看到 viewBox 设置部分,视觉上被放大。

2.2、SVG 如何嵌入 HTML

SVG 的代码可以直接嵌入到 html 页面中,也可以通过 html 的embed、object、iframe嵌入到html中。嵌入的时候嵌入的是 SVG 文件,SVG 文件必须使用 .svg 后缀。分别介绍各种方法如何使用?

2.2.1、embed 嵌入:

使用语法:<embed src="line.svg" type="image/svg+xml"></embed>

src是SVG文件路径,type 表示 embed 引入文件类型。

优点:所有浏览器都支持,并允许使用脚本。

缺点:不推荐 html4 和 html 中使用,但 html5 支持。

2.2.2、object 嵌入:

使用语法:<object data="line.svg" type="image/svg+xml"></object>

data 是 SVG 文件路径,type 表示 object 引入文件类型。

优点:所有浏览器都支持,支持 html、html4 和 html5。

缺点:不允许使用脚本。

2.2.3、iframe 嵌入:

使用语法:<iframe width="300" height="300" src="./line.svg" frameborder="0"></iframe>

src是 SVG 文件路径,width、height、frameborder 设置的大小和边框。

优点:所有浏览器都支持,并允许使用脚本。

缺点:不推荐 html4 和 html 中使用,但 html5 支持。

2.2.4、html中嵌入:

svg 标签直接插入 html 内容内,与其他标签用法一致。

2.2.5、连接到svg文件:

使用 a 标签,直接链接到 SVG 文件。

使用语法:<a href="line.svg">查看SVG</a>

三、SVG形状元素

3.1、线 - line

使用语法:
<svg width="300" height="300" >  
 <line x1="0" y1="0" x2="300" y2="300" stroke="black" stroke-width="20"></line>
</svg>

使用line标签创建线条,(x1,y1)是起点,(x2,y2)是终点,stroke绘制黑线,stroke-width是线宽。

3.2、矩形 - rect

//使用语法:
<svg width="300" height="300" >
<rect 
 width="100" height="100"  //大小设置
 x="50" y="50"  //可选 左上角位置,svg的左上角默认(0,0)
 rx="20" ry="50" //可选 设置圆角
 stroke-width="3" stroke="red" fill="pink" //绘制样式控制
></rect>
</svg>

上述参数 width、height是必填参数,x、y是可选参数,如不设置的时候,默认为(0,0),也就是svg的左上角开始绘制。rx、ry是可选参数,不设置是矩形没有圆角。fill定义填充颜色。

3.3、圆形 - circle

// 使用语法
<svg width="300" height="300" >
 <circle 
  cx="100" cy="50" // 定义圆心 ,可选
  r="40" // 圆的半径
  stroke="black" stroke-width="2" fill="red"/> //绘制黑框填充红色
</svg>

上述(cx,xy)定义圆心的位置,是可选参数,如果不设置默认圆心是(0,0)。r是必需参数,设置圆的半径。

3.4、椭圆 - ellipse

椭圆与圆相似,不同之处在于椭圆有不同的x和y半径,而圆两个半径是相同的。

// 使用语法
<svg width="300" height="300" >
 <ellipse 
  rx="20" ry="100" //设置椭圆的x、y方向的半径
  fill="purple" // 椭圆填充色
  cx="150" cy="150" //设置椭圆的圆心 ,可选参数
 ></ellipse>
</svg>

上述椭圆的两个rx、ry两个方向半径是必须参数,如果rx=ry就表示是圆形,(cx,cy)是椭圆的圆心,是可选参数,如果不设置,则默认圆心为(0,0)。

3.5、折线 - polyline

// 使用语法
<svg width="300" height="300" style="border:solid 1px red;">
  <!-- 绘制出一个默认填充黑色的三角形 -->
 <polyline 
  points=" //点的集合
   0 ,0, // 第一个点坐标
   100,100, // 第二个点坐标
   100,200 // 第三个点坐标
    " 
  stroke="green" 
 ></polyline>
<!-- 绘制一个台阶式的一条折线 -->
 <polyline 
  points="0,0,50,0,50,50,100,50,100,100,150,100,150,150" 
  stroke="#4b27ff" fill="none"
 ></polyline>
</svg>

上述代码执行结果如图所示:

需要注意的是 points 中包含了多个点的坐标,但不是一个数组。

3.6、多边形 - polygon

polygon 标签用来创建不少于3个边的图形,多边形是闭合的,即所有线条连接起来。

// 使用语法
<svg width="300" height="300" style="border:solid 1px red;">
 <polygon 
  points="
    0,0,   //多边形的第一点
   100,100,  //多边形的第二点
    0,100  //多边形的第三点
  " 
	stroke="purple"
	stroke-width="1"
	fill="none"
 ></polygon>
</svg>

polygon绘制的时候与折线有些类似,但是polygon会自动闭合,折线不会。

3.7、路径 - path

path 是SVG基本形状中最强大的一个,不仅能创建其他基本形状,还能创建更多其他形状,如贝塞尔曲线、2次曲线等。

点个关注,下篇更精彩!