怎么用js实现图片点击时放大,再点击恢复

JavaScript08

怎么用js实现图片点击时放大,再点击恢复,第1张

用js实现图片点击时放大,再点击恢复的方法:

1、页面定义区片区域:

<img src="Images/home.jpg" id="Img1" width="118" height="106" border="0">

<img src="Images/machine.jpg" id="Img2" width="118" height="106" border="0">

<img src="Images/title_Mixie.jpg" id="Img3" width="118" height="106" border="0">

2、定义js方法的mouseover和mouseout事件:

$(document).ready(function () {

$('#Img1, #Img2, #Img3').mouseover(function () {

$(this).animate({ width: "122px", height: "110px" }, 100)

})当鼠标放上去的时候,图片变大

$('#Img1, #Img2, #Img3').mouseout(function () {

$(this).animate({ width: "118px", height: "106px" }, 100)

})当鼠标离开的时候,图片还原为原来的尺寸

})

1、首先选择要编辑的两张图片,点击右键选择编辑

2、先编辑要作为底图的图片,把作为贴图黏贴到另一张图片的部分截取出来,点击复制

3、进入另一张图片的编辑页面,点击黏贴,之后调整一下图片未知即可。

一、案例需求

点击按钮后图片隐藏,同时按钮的文字变为显示,再次点击按钮图会显示,同时按钮的文

字变为隐藏。

二、案例分析

1、事件源:按钮(button)

2、事件类型:点击(onclick)

3、事件处理程序:

(1)文字内容改变用:innerText

(2)图片是否显示用:display

三、代码如下

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>显示和隐藏图片</title>

<style>

#new {

width: 200px

margin-top: 10px

}

</style>

</head>

<body>

<button id="btn">隐藏</button><br>

<img src="images/img01.jpg" id="new">

<script>

// 1. 获取元素

var btn = document.querySelector('#btn')

var img = document.querySelector('#new')

var index = 1

// 2. 添加事件

btn.onclick = function() {

//法一:

if (index++ % 2 === 1) {

img.style.display = 'none'

this.innerText = '显示'

} else {

img.style.display = 'block'

this.innerText = '隐藏'

}

// 法二

// if (this.innerText === '隐藏') {