Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
前面兩篇文章,學(xué)習(xí)了JavaScript中DOM、BOM相關(guān)基礎(chǔ)知識后,今天我們將結(jié)合前面所學(xué)內(nèi)容,利用DOM操作實現(xiàn)常見的網(wǎng)頁動畫效果;結(jié)合知識點有相關(guān)案例效果演示,另外本文所設(shè)計的源碼和配套素材可在文末獲??;
獲得元素距離帶有定位父元素的位置
獲得元素自身的width和height
思路:盒子內(nèi)的坐標(biāo)可以通過鼠標(biāo)事件的e.pageX獲取到頁面坐標(biāo)再減去element.offsetLeft來得到;
var box = document.querySelector('.box');
box.addEventListener('click',function(e){
var x = e.pageX - this.offsetLeft;
var y = e.pageY - this.offsetTop;
console.log('x:'+x+',y:'+y);
})
需求:
(1)點擊彈出層,會淡出模塊框,并且顯示灰色半透明的遮擋層;
(2)點擊關(guān)閉按鈕,可以關(guān)閉模態(tài)框,并且同時關(guān)閉灰色半透明遮擋層;
(3)鼠標(biāo)放到模態(tài)框最上面一行,可以按住鼠標(biāo)拖拽模態(tài)框在頁面中移動;
(4)鼠標(biāo)松開,可以停止拖動模態(tài)框移動;
分析:
(1)拖拽事件,鼠標(biāo)是按著的狀態(tài),故mousemove和mouseup須卸載mousedown事件里面;
(2)在拖拽的過程中,鼠標(biāo)的位置相對于組件的坐標(biāo)是不變的,因此為了改變組件的位置,需要將頁面坐標(biāo)減去相對組件坐標(biāo)來獲??;
var link = document.querySelector('#link');
var login_window = document.querySelector('#login');
var close_button = document.querySelector('#closeBtn');
var bg = document.querySelector('#bg');
link.addEventListener('click',function(){
login_window.style.display = 'block';
bg.style.display = 'block';
})
close_button.addEventListener('click',function(){
login_window.style.display = 'none';
bg.style.display = 'none';
})
// 拖動事件三個過程:mousedown、mousemove、mouseup
var title = document.querySelector('#title');
title.addEventListener('mousedown',function(e){
var x = e.pageX - login_window.offsetLeft;
var y = e.pageY - login_window.offsetTop;
document.addEventListener('mousemove',move);
function move(e){
login_window.style.left = e.pageX - x + 'px';
login_window.style.top = e.pageY -y + 'px';
}
document.addEventListener('mouseup',function (e) {
document.removeEventListener('mousemove',move);
});
});
分析:
(1)mask界面與big界面可以通過鼠標(biāo)事件設(shè)置dispaly屬性即可;mouseover顯示mask,mouseleave隱藏mask、mousemove移動mask
(2)mask的left與top的值可以通過鼠標(biāo)在prew_img中的位置來獲?。?/span>
(3)為了讓mask界面中央跟著鼠標(biāo)指針走,則需要減去mask尺寸一半的偏移量;
(4)防止mask頁面跑出prew_img,需要對偏移量進行判斷;
(5)大圖片移動距離怎么計算?mask移動距離/mask最大移動距離 = 大圖片移動距離/大圖片最大移動距離;所以大圖片移動距離 = mask移動距離 * 大圖片移動最大距離 / 遮擋層最大移動距離;
window.addEventListener('load',function(){
var prew_img = document.querySelector('.preview_img');
var mask = document.querySelector('.mask');
var big = document.querySelector('.big');
prew_img.addEventListener('mouseover',function(){
mask.style.display = 'block';
big.style.display = 'block';
})
prew_img.addEventListener('mousemove',function(e){
// 獲取鼠標(biāo)在prew_img元素內(nèi)的坐標(biāo)
var x = e.pageX - this.offsetLeft;
var y = e.pageY - this.offsetTop;
// 防止mask頁面跑出prew_img盒子
var maskX = x - mask.offsetWidth / 2;
// 遮擋層的最大移動距離maskMax
var maskMax = this.offsetWidth - mask.offsetWidth;
if(maskX <= 0){
maskX = 0;
}else if(maskX >= maskMax){
maskX = maskMax;
}
var maskY = y - mask.offsetHeight / 2;
if(maskY <= 0){
maskY = 0;
}else if(maskY >= maskMax){
maskY = maskMax;
}
// 讓mask界面中央跟著鼠標(biāo)指針走
mask.style.left = maskX + 'px';
mask.style.top = maskY + 'px';
// 讓big界面內(nèi)容跟著mask位置移動,
// 大圖片移動距離 = mask移動距離 * 大圖片移動最大距離 / mask最大移動距離
var big_img = document.querySelector('.bigImg');
var bigMax = big_img.offsetWidth - big.offsetWidth;
// 大圖片移動距離
var bigMoveX = maskX * bigMax / maskMax;
var bigMoveY = maskY * bigMax / maskMax;
big_img.style.left = -bigMoveX + 'px';
big_img.style.top = -bigMoveY + 'px';
});
prew_img.addEventListener('mouseleave',function(){
mask.style.display = 'none';
big.style.display = 'none';
});
});
不需要調(diào)用,立馬能自己執(zhí)行的函數(shù);
// 寫法一:
(function(){})()
// 寫法二:
(function(){}())
立即執(zhí)行函數(shù)也可以傳遞參數(shù):
(function(a,b){
console.log(a+b);
})(1,2)
立即執(zhí)行函數(shù)的作用是獨立創(chuàng)建了一個作用域,里面所有的變量都是局部變量,不會有命名沖突的情況;
使用scroll系列相關(guān)屬性可以動態(tài)得到元素的大小、滾動距離;
scroll系列屬性 作用 備注 element.scrollTop 返回被卷去的上側(cè)距離 返回的數(shù)值不帶單位 element.scrollLeft 返回被卷去的左側(cè)距離 返回的數(shù)值不帶單位 element.scrollWidth 返回自身的實際寬度,不含邊框 返回的數(shù)值不帶單位 element.scrollHeight 返回自身的實際高度,不含邊框 返回的數(shù)值不帶單位
頁面被卷去的頭部可以通過:window.pageYOffset來獲得
// 1.獲取元素
var sliderBar = document.querySelector('.slider-bar');
var topBar = document.querySelector('.header');
var banner = document.querySelector('.banner');
var main = document.querySelector('.main');
var goBack = document.querySelector('.goBack');
var barHeight = banner.offsetTop;
var sliderBarTop = sliderBar.offsetTop;
var fixTop = sliderBarTop - barHeight;
var mainTop = main.offsetTop;
// 2.頁面滾動事件
document.addEventListener('scroll',function(){
if(window.pageYOffset >= barHeight){
sliderBar.style.position = 'fixed';
sliderBar.style.top = fixTop + 'px';
}else{
sliderBar.style.position = 'absolute';
sliderBar.style.top = sliderBarTop + 'px';
}
if(window.pageYOffset >= mainTop){
goBack.style.display = 'block';
}else{
goBack.style.display = 'none';
}
})
當(dāng)鼠標(biāo)移動到元素上時就會觸發(fā)mouseenter事件,類似mouseenter
兩者的區(qū)別是:mouseover鼠標(biāo)經(jīng)過自身盒子會觸發(fā)、經(jīng)過子盒子也會觸發(fā);而mouseenter只有經(jīng)過自身盒子才會觸發(fā);
原因是mouseenter不會冒泡;跟mouseenter搭配的通常是mouseleave
通過定時器setInterval()不斷移動盒子位置;
實現(xiàn)步驟:
//利用setInterval設(shè)置一個box元素從左向右移動400像素的動畫
var box = document.querySelector('.box');
var timer = setInterval(function(){
if(box.offsetLeft > 200){
// 停止動畫,本質(zhì)上是清除定時器
clearInterval(timer);
}
box.style.left = box.offsetLeft + 5 + 'px';
},50);
將動畫函數(shù)封裝起來,利用JS是動態(tài)語言的特性,通過設(shè)置對象的方式,給每個不同的元素指定不同的定時器;
// 簡單動畫函數(shù)的封裝,目標(biāo)對象obj,目標(biāo)位置target
function animate(obj,target){
// 如果設(shè)置一個按鈕啟動動畫,會存在一個bug,只要不斷點擊按鈕,動畫會越走越快;
// 原因是啟動了太多定時器,解決方案是,讓元素只有一個定時器執(zhí)行
clearInterval(obj.timer);
obj.timer = setInterval(function(){
if(obj.offsetLeft > target){
// 停止動畫,本質(zhì)上是清除定時器
clearInterval(obj.timer);
}
obj.style.left = obj.offsetLeft + 5 + 'px';
},50);
}
//利用setInterval設(shè)置動畫
var btn = document.querySelector('button');
var box1 = document.querySelector('.box1');
btn.addEventListener('click',function(){
animate(box1,400);
});
緩動動畫就是讓元素運動速度有所變換,最常見的就是讓速度慢慢停下來;
思路:讓盒子每次移動的距離慢慢變小;
核心算法:每次移動的步長 = (目標(biāo)值-現(xiàn)在的位置)/10這里的10是份數(shù)可以是9也可以是8;
停止的條件是:當(dāng)前盒子的位置等于目標(biāo)位置就停止定時器;
// 緩動動畫
function animate(obj,target){
// 清除定時器
clearInterval(obj.timer);
obj.timer = setInterval(function() {
// 每次運行定時器都要重新計算步長值,所以步長值寫在定時器里面
var step = (target - obj.offsetLeft)/10;
// 如果step是正值則往大取整,如果step是負(fù)值則往小取整
step = step >= 0 ? Math.ceil(step) : Math.floor(step);
if(obj.offsetLeft == target){
clearInterval(obj.timer);
}
// 緩動動畫核心算法:前進步長=(目標(biāo)-當(dāng)前)/ 10
obj.style.left = obj.offsetLeft + step + 'px';
},50);
}
回調(diào)函數(shù)原理:函數(shù)可以作為一個參數(shù),將這個函數(shù)作為參數(shù)傳入到另一個函數(shù)里面,當(dāng)這個函數(shù)執(zhí)行完成后,再執(zhí)行傳進去的這個函數(shù),這個過程叫做回調(diào)。
回調(diào)函數(shù)位置:寫到定時器結(jié)束的位置
// 緩動動畫
function animate(obj,target,callback){
// 清除定時器
clearInterval(obj.timer);
obj.timer = setInterval(function() {
// 每次運行定時器都要重新計算步長值,所以步長值寫在定時器里面
var step = (target - obj.offsetLeft)/10;
// 如果step是正值則往大取整,如果step是負(fù)值則往小取整
step = step >= 0 ? Math.ceil(step) : Math.floor(step);
if(obj.offsetLeft == target){
clearInterval(obj.timer);
if(callback){
// 調(diào)用函數(shù)
callback();
}
}
// 緩動動畫核心算法:前進步長=(目標(biāo)-當(dāng)前)/ 10
obj.style.left = obj.offsetLeft + step + 'px';
},50);
}
在head里引入JS文件
<script src="animate.js"></script>
實際調(diào)用過程中可以直接調(diào)用JS中定義的函數(shù)
var sliderbar = document.querySelector('.sliderbar');
var con = document.querySelector('.con');
sliderbar.addEventListener('mouseenter',function(){
animate(con,-160,function () {
// 動畫執(zhí)行完畢,將左箭頭轉(zhuǎn)為右箭頭
sliderbar.children[1].innerHTML = '→';
});
})
sliderbar.addEventListener('mouseleave',function(){
animate(con,0,function () {
sliderbar.children[1].innerHTML = '←';
});
})
功能需求:
window.addEventListener('load', function () {
// === 0.定義變量,獲取元素
var focus = document.querySelector('.focus');
var arrowL = document.querySelector('.arrow-l');
var arrowR = document.querySelector('.arrow-r');
// === 1.鼠標(biāo)經(jīng)過輪播圖模塊,左右按鈕顯示,離開隱藏左右按鈕;=====
focus.addEventListener('mouseenter', function () {
arrowL.style.display = 'block';
arrowR.style.display = 'block';
clearInterval(timer);
timer = null;
});
focus.addEventListener('mouseleave', function () {
arrowL.style.display = 'none';
arrowR.style.display = 'none';
timer = window.setInterval(function () {
arrowR.click();
}, 3000);
});
// === 2.根據(jù)輪播圖的數(shù)量自動生成小圓圈
var ul = focus.querySelector('ul');
var ol = focus.querySelector('.circle');
var pictureWidth = focus.offsetWidth;
// 獲取到輪播圖圖片的數(shù)量length
var length = ul.children.length;
// 顯示當(dāng)前圖片的索引
var circle = 0;
// 顯示當(dāng)前圖片的索引
var num = 0;
// 創(chuàng)建元素插入到ol中
for (var i = 0; i < length; i++) {
var li = document.createElement('li');
// 給沒一個小圓圈添加一個index屬性,方便點擊事件的ul移動長度的確定
li.setAttribute('index', i);
// 給每一個小圓圈添加點擊事件
li.addEventListener('click', function () {
// 移除其他小圓圈的選中屬性
for (var i = 0; i < length; i++) {
ol.children[i].className = '';
}
// 設(shè)置當(dāng)前選中小圓圈的選中屬性
this.className = 'current';
// 定義選中的小圓圈的索引
var index = this.getAttribute('index');
num = index;
circle = index;
animate(ul, -pictureWidth * index);
});
ol.appendChild(li);
}
ol.children[0].className = 'current';
// === 3.鼠標(biāo)點擊右側(cè)按鈕一次,圖片往左播放一張,以此內(nèi)推,左側(cè)按鈕同理;==
// 為了實現(xiàn)無縫滾動,需要將第一張圖添加到最后一張圖中
var lastLi = ul.children[0].cloneNode(true);
ul.appendChild(lastLi);
// 設(shè)置一個節(jié)流閥,防止按鈕在短時間內(nèi)點擊過多
var flag = true;
arrowR.addEventListener('click', function () {
if (flag) {
flag = false;
if (num == ul.children.length - 1) {
ul.style.left = 0;
num = 0;
}
num++;
animate(ul, -pictureWidth * num, function () {
flag = true;
});
circle++;
if (circle == ol.children.length) {
circle = 0;
}
activateCircle(circle);
}
});
arrowL.addEventListener('click', function () {
if (flag) {
flag = false;
if (num == 0) {
num = ul.children.length - 1;
ul.style.left = -num * pictureWidth + 'px';
}
num--;
animate(ul, -num * pictureWidth, function () {
flag = true;
});
if (circle == 0) {
circle = ol.children.length;
}
circle--;
activateCircle(circle);
}
});
// 排他法思想點亮小圓圈
function activateCircle(index) {
for (var i = 0; i < ol.children.length; i++) {
ol.children[i].className = '';
}
ol.children[index].className = 'current';
}
// 實現(xiàn)輪播圖自動播放
var timer = window.setInterval(function () {
arrowR.click();
}, 3000);
});
顯示效果:
輪播圖自動播放的原理是:設(shè)置一個定時器使用click()函數(shù)模擬按鈕點擊事件;
輪播圖實現(xiàn)無縫滾動的原理是:將第一個圖動態(tài)加在最后一張圖上,當(dāng)切到最后一張圖時,重新設(shè)置元素布局距離,再執(zhí)行滑動動畫。
防止同一個按鈕在短時間內(nèi)點擊次數(shù)過多,可以設(shè)置一個節(jié)流閥,將啟動節(jié)流閥的操作寫在動畫回調(diào)函數(shù)里,這樣只有當(dāng)動畫執(zhí)行完畢后才才可以重新恢復(fù)按鈕的點擊功能;
頁面滾動動畫可以使用函數(shù):window.scroll(x,y)可以滾動至頁面的指定坐標(biāo)位置;
最后:
本文配套源碼與素材獲取:https://gitee.com/yushengtan/jscode
關(guān)于JavaScript在網(wǎng)頁動畫中的使用就介紹到這里,下一節(jié)我們將繼續(xù)深入介紹JavaScript的異步調(diào)用相關(guān)知識的學(xué)習(xí)~
需為從Web服務(wù)器,外部CDN或第三方Javascript插件中加載資產(chǎn)而為Web項目創(chuàng)建加載動畫的最佳方法之一,就是使用與渲染相同的Web技術(shù)在Web應(yīng)用程序中創(chuàng)建動畫。
這樣可以確保動畫在你需要時立即顯示,并確保用戶體驗不會因用戶連接速度而受到影響。
在本教程中,我們將使用HTML和CSS為Web項目制作一個簡單的加載動畫。
我們追求的最終結(jié)果是以下動畫。
添加以下html模板:
<div class="svg-loader">
<svg class="svg-container" height="100" width="100" viewBox="0 0 100 100">
<circle class="loader-svg bg" cx="50" cy="50" r="45"></circle>
<circle class="loader-svg animate" cx="50" cy="50" r="45"></circle>
</svg>
</div>
從上面的插圖可以明顯看出,動畫模板由兩個圓圈組成,一個圓圈在另一個圓圈的上方,第一個圓圈的厚度大于第二個圓圈的圓圈,兩個圓圈都具有相同的圓周,從而使一個圓圈在另一個圓圈內(nèi)的錯覺。
圓內(nèi)的cx和cy屬性分別是兩個圓的x軸和y軸坐標(biāo)。他們確保兩個圓以同一坐標(biāo)為中心。
添加以下css:
.svg-loader{
display:flex;
position: relative;
align-content: space-around;
justify-content: center;
}
.loader-svg{
position: absolute;
left: 0; right: 0; top: 0; bottom: 0;
fill: none;
stroke-width: 5px;
stroke-linecap: round;
stroke: rgb(64, 0, 148);
}
.loader-svg.bg{
stroke-width: 8px;
stroke: rgb(207, 205, 245);
}
上面的css確保加載組件位于容器的中心,并確保兩個圓具有不同的筆觸寬度和顏色。
然后,我們用CSS動畫第二個圓,該圓將位于第一個圓的頂部,以完成加載動畫:
.animate{
stroke-dasharray: 242.6;
animation: fill-animation 1s cubic-bezier(1,1,1,1) 0s infinite;
}
@keyframes fill-animation{
0%{
stroke-dasharray: 40 242.6;
stroke-dashoffset: 0;
}
50%{
stroke-dasharray: 141.3;
stroke-dashoffset: 141.3;
}
100%{
stroke-dasharray: 40 242.6;
stroke-dashoffset: 282.6;
}
}
當(dāng)我們繪制圓時,它只是一個單點劃線,畫出了從圓頭到圓的形狀輪廓,stroke-dasharray屬性使我們能夠?qū)⑵浞纸鉃辄c劃線和間隙。我們可以利用此功能來獲得所需的動畫最終結(jié)果。
為了獲得如上所示的平滑動畫效果,我們需要知道圓的圓周,其中圓周= 2 x pi x radius。然后,我們使用stroke-dasharray屬性在動畫的不同狀態(tài)下繪制最多一個破折號和間隙,以交替改變其大小,同時在添加兩者時保持圓周長度。
圓半徑為45時,周長為282.6,因此將stroke-dasharray的值設(shè)置為141.3時,意味著虛線和間隙的值相同,其總和為282.6。
dashoffset屬性是定義在相關(guān)虛線數(shù)組的呈現(xiàn)的偏移呈現(xiàn)屬性,該偏移給出到加載動畫的旋轉(zhuǎn)作用,否則動畫將出現(xiàn)破裂。這就是填充動畫動畫中發(fā)生的所有事情。
我們通過使用.animation類將動畫應(yīng)用于第二個圓環(huán)來確保該動畫無限進行。
這只是使用SVG和CSS可以制作動畫的基礎(chǔ),可以進行實驗性和創(chuàng)造性的創(chuàng)作,從而為你的Web項目創(chuàng)建快速且像素完美的加載動畫。
如果你喜歡這篇文章,請給我點贊,收藏,轉(zhuǎn)發(fā),這是對我真的很重要
關(guān)注我長期分享前端小技巧
<script src="https://lf6-cdn-tos.bytescm.com/obj/cdn-static-resource/tt_player/tt.player.js?v=20160723"></script>
下載這套模板打開之后,感覺眼前一亮,很久沒看到這么精致的PPT模板了,這套PPT有那些特點呢?
下面給大家詳細(xì)的介紹一下:
首先我們先說一下有多少頁,這套PPT單套模板是120頁,夠用了吧!
并且包含了兩種格式PPTX和PPT,可以兼容不同版本的OFFICE使用,每種版本下面包含了兩種底色:深色+淺色每種底色下面包含了兩種形式:動畫和靜態(tài)模板,每種形態(tài)下包含了6種配色主題風(fēng)格。
精致創(chuàng)意的圖形設(shè)計,PPT模板采用16:9超高清尺寸規(guī)格,并且為每一個圖形設(shè)置了精美的動畫效果。
PPT內(nèi)的圖形均為矢量格式,可以自由放大縮小不失真,并且可以自行修改編輯顏色,形狀,大小等。
效果預(yù)覽中的人物,背景圖等不包含在文件內(nèi),但包含圖片占位符,可一鍵換圖。
本套PPT模板的下載地址為
https://www.gfxaa.com/4563.html
PPT模板整體效果圖預(yù)覽
歡迎關(guān)注頂尖PPT
分享來自全球最頂尖,最時尚的keynote、Powerpoint模板
*請認(rèn)真填寫需求信息,我們會在24小時內(nèi)與您取得聯(lián)系。