
樣式
```
<style>
#parent{
width:540px;
height: 215px;
position: relative;
border:1px solid #000;
}
#parent>img{
width:500px;
padding: 20px;
}
#fade{
width: 540px;
height: 215px;
position: absolute;
left: 0px;
top:0px;
background: #4a4266;
opacity: 0.4;
}
</style>
```
結構
```
<body>
<div id="parent">
<img src="images/01.jpg" alt="">
<div id="fade"></div>
</div>
</body>
```
JS(未進行函數封裝)
```
var parent = document.getElementById("parent");
var fade = document.getElementById("fade");
var opacity = getComputedStyle(fade).opacity*100;
var timer;
// 未封裝
parent.onmouseover = function(){
clearInterval(timer);
//如果不清除則鼠標移上幾次就會增加幾個定時器,每個定時器執行下面代碼,透明度加快
timer = setInterval(function(){
if(opacity == 70){
clearInterval(timer);
//停止繼續累加透明度
}
else{
opacity += 2;
fade.style.opacity = opacity/100;
console.log(opacity);
}
},100)
}
parent.onmouseout = function(){
clearInterval(timer);
timer = setInterval(function(){
if(opacity == 40){
clearInterval(timer);
}
else{
opacity -= 2;
fade.style.opacity = opacity/100;
}
},100)
}
```
封裝函數
```
parent.onmouseover = function(){
opacity_change(90,2);
}
parent.onmouseout = function(){
opacity_change(40,-2);
}
function opacity_change(reach,add){
clearInterval(timer);
timer = setInterval(function(){
if(opacity == reach){
clearInterval(timer);
}
else{
opacity += add;
console.log(opacity/100);
fade.style.opacity = opacity/100;
}
},500)
}
```
- 1.JS的基礎知識
- (1)調試
- (2)變量
- (3)數據類型
- 數據類型之間的轉換
- (4)全局變量和局部變量
- (5)運算符和表達式
- (6)數組
- 2.控制語句DOM,BOM,事件
- (1)控制語句
- (2)DOM的基礎
- 節點
- 改變樣式
- DOM事件
- 3.函數
- (1)聲明函數
- (2)構造函數
- (3)函數的參數
- (4)函數的傳參
- (5)改變this
- (6)重載
- (7)回調函數
- 4.數組
- (1)創建數組
- (2)增刪改查
- (3)字符串與數組的轉換
- 5.正則
- (1)創建正則
- (2)字符串中支持正則
- (3)語法
- 最核心的元字符
- 6.ajax
- (1)原生ajax
- (2)http,get,post
- (3)跨域
- (4)jQuery-ajax
- (5)axios
- 7.面向對象
- (1)原型
- (2)原型鏈,繼承
- (3)多態
- 8.es6小結
- 9.js+canvas實現驗證碼
- 10.js的作用域
- 11.閉包
- 實例
- toggle
- 圖片切換
- swiper
- 遮罩顏色漸變
- 表格添加
- 瀑布流
- ajax數據請求渲染
- 百度地圖