---
title: "JQuery开发2-网页内容淡入淡出"
author: "舟舟同学"
date: "2016-05-25T15:32:52Z"
tags: ["jQuery"]
categories: ["编程语言"]
slug: "jquery-2"
draft: false
lastmod: "2016-05-25T15:41:36Z"
id: 2300855
url: "https://673390302.iteye.com/blog/2300855"
---

在网页中，用网页内容淡入淡出可以使得网页内容更加生动，在jQuery中实现起来比较简单，用的方法主要是这几个

（1） fadeIn() 用于淡入已隐藏的元素。

（2）jQuery fadeOut()  方法用于淡出可见元素。

（3）jQuery fadeToggle() 方法

jQuery fadeToggle() 方法可以在 fadeIn() 与 fadeOut() 方法之间进行切换。如果元素已淡出，则 fadeToggle() 会向元素添加淡入效果。如果元素已淡入，则 fadeToggle() 会向元素添加淡出效果。

(4)   jQuery fadeTo(speed,opacity,callback) 其中， speed 参数规定效果的时长。它可以取以下值："slow"、"fast" 或毫秒。opacity 参数将淡入淡出效果设置为给定的不透明度（值介于 0 与 1 之间）。可选的 callback 参数是该函数完成后所执行的函数名称。

具体实现代码如下：

 

 

 

```
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <script language="javascript" src="${pageContext.request.contextPath}/script/jquery.js"></script>
  <script>
$(document).ready(function(){
  $("#b").click(function(){
    $("#div1").fadeIn(1000);
    $("#div2").fadeIn(2000);
    $("#div3").fadeIn(3000);
    });
    $("#b1").click(function()
    {
    $("#div1").fadeOut(1000);
    $("#div2").fadeOut(2000);
    $("#div3").fadeOut(3000);
    });
});
</script>
</head>
<body>
<button id="b">点我，使三个矩形淡入</button>
<button id="b1">点我，使三个矩形淡出</button>
<br><br>
<div id="div1" style="width:80px;height:80px;display:none;background-color:red;"></div>
<br>
<div id="div2" style="width:80px;height:80px;display:none;background-color:green;"></div>
<br>
<div id="div3" style="width:80px;height:80px;display:none;background-color:blue;"></div>
</body>
</html>
```

或者用fadeToggle(),代码如下：

```
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <script language="javascript" src="${pageContext.request.contextPath}/script/jquery.js"></script>
  <script>
$(document).ready(function(){
  $("#b").click(function(){
    $("#div1").fadeToggle(1000);
    $("#div2").fadeToggle(2000);
    $("#div3").fadeToggle(3000);
    });
    $("#b1").click(function()
    {
    $("#div1").fadeToggle(1000);
    $("#div2").fadeToggle(2000);
    $("#div3").fadeToggle(3000);
    });
});
</script>
</head>
<body>
<button id="b">点我，使三个矩形淡入</button>
<button id="b1">点我，使三个矩形淡出</button>
<br><br>
<div id="div1" style="width:80px;height:80px;display:none;background-color:red;"></div>
<br>
<div id="div2" style="width:80px;height:80px;display:none;background-color:green;"></div>
<br>
<div id="div3" style="width:80px;height:80px;display:none;background-color:blue;"></div>
</body>
</html>
```

 

 实现三个不同颜色的矩阵在网页中的淡入和淡出。