• <\/div>
    <\/div>
    <\/div>
    <\/div><\/div><\/body><\/html><\/pre>

    事件監(jiān)聽<\/h2>

    此時(shí)窗口里只顯示一個(gè)頁面,我們給其加上滾動(dòng)監(jiān)聽,因?yàn)閒irefox和非firefox瀏覽器對(duì)滾動(dòng)監(jiān)聽支持不同,firefox瀏覽器向上滾動(dòng)是-120,向下滾動(dòng)是120,而其他瀏覽器向上是5,向下是-5,所以需要作判斷:<\/p>

    	
    
    
    
    
    
    
    

    国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

    目錄
    頁面結(jié)構(gòu)
    事件監(jiān)聽
    加入動(dòng)畫
    首頁 web前端 html教學(xué) CSS3實(shí)現(xiàn)整屏切換效果_html/css_WEB-ITnose

    CSS3實(shí)現(xiàn)整屏切換效果_html/css_WEB-ITnose

    Jun 24, 2016 am 11:42 AM

    總是能看見很多廣告或者網(wǎng)站都是使用整屏滾動(dòng)的效果,一直看著都心癢癢,想自己也實(shí)現(xiàn)一個(gè)。最近剛學(xué)習(xí)到css3的動(dòng)畫效果,所以嘗試使用css3做了一個(gè)整屏切換。

    頁面結(jié)構(gòu)

    實(shí)現(xiàn)思路與大眾方法類似,如圖

    每個(gè)section就是一頁內(nèi)容,它的大小充滿了屏幕(紅色區(qū)域),一個(gè)container由多個(gè)section構(gòu)成,我們通過改變container的位置,來達(dá)到頁面切換的效果。container向下走,頁面好像上移了,container向上走,頁面就下移了。
    html結(jié)構(gòu)如下:

    <!DOCTYPE html><html><head lang="ch">    <meta charset="UTF-8">    <!--適配移動(dòng)端-->    <meta name=”viewport” content="width=device-width, user-scalable=no, initial-scale=1.0">    <title></title>    <style> body, html{ padding: 0; margin: 0; } body, html { height: 100%; /**隱藏滾動(dòng)條**/ overflow: hidden; } #container, .section { height: 100%; } #section0 { background-color: #83af9b; } #section1 { background-color: #764d39; } #section2 { background-color: #ff9000; } #section3 { background-color: #380d31; } </style></head><body><div id="container">    <div class="section" id="section0"></div>    <div class="section" id="section1"></div>    <div class="section" id="section2"></div>    <div class="section" id="section3"></div></div></body></html>

    事件監(jiān)聽

    此時(shí)窗口里只顯示一個(gè)頁面,我們給其加上滾動(dòng)監(jiān)聽,因?yàn)閒irefox和非firefox瀏覽器對(duì)滾動(dòng)監(jiān)聽支持不同,firefox瀏覽器向上滾動(dòng)是-120,向下滾動(dòng)是120,而其他瀏覽器向上是5,向下是-5,所以需要作判斷:

    <script src="http://code.jquery.com/jquery-latest.js"></script><script> //當(dāng)前頁面索引 var curIndex = 0; var scrollFunc = function (e) { e = e || window.event; var t = 0; if (e.wheelDelta) {//IE/Opera/Chrome t = e.wheelDelta; if (t > 0 && curIndex > 0) { //上滾動(dòng) movePrev(); } else if (t < 0 && curIndex < sumCount - 1) { //下滾動(dòng) moveNext(); } } else if (e.detail) {//Firefox t = e.detail; if (t < 0 && curIndex > 0) { //上滾動(dòng) movePrev(); } else if (t > 0 && curIndex < sumCount - 1) { //下滾動(dòng) moveNext(); } } }; function moveNext(){ } function movePrev(){ } function init(){ /*注冊(cè)事件*/ if (document.addEventListener) { document.addEventListener('DOMMouseScroll', scrollFunc, false); }//W3C window.onmousewheel = document.onmousewheel = scrollFunc;//IE/Opera/Chrome } init(); </script>

    為了防止在第一個(gè)頁面用戶上滾,最后一個(gè)頁面用戶下滾,所以用curIndex代表當(dāng)前頁面索引在滾動(dòng)時(shí)作了監(jiān)聽,當(dāng)然如果要使頁面循環(huán)滾動(dòng),只需修改條件限制即可。

    加入動(dòng)畫

    動(dòng)畫使用到了css3里的transform屬性的translate3D,我們首先需要獲取到屏幕的高度,然后當(dāng)頁面切換的時(shí)候?qū)ontainer上移一個(gè)屏幕高度或下移一個(gè)屏幕高度。
    使用translate3D的原因是在手機(jī)端會(huì)開啟硬件加速,使動(dòng)畫更流暢,它接收三個(gè)參數(shù),分別是x軸、y軸和z軸的位移。如

    transform: tanslate3D(10px, 30px, 0);

    修改后的js代碼如下:

    <script> //當(dāng)前頁面索引 var curIndex = 0; //container元素 var container = $("#container"); //頁面總數(shù) var sumCount = $(".section").length; //窗體元素 var $window = $(window); //動(dòng)畫時(shí)間 var duration = 500; var scrollFunc = function (e) { e = e || window.event; var t = 0; if (e.wheelDelta) {//IE/Opera/Chrome t = e.wheelDelta; if (t > 0 && curIndex > 0) { //上滾動(dòng) movePrev(); } else if (t < 0 && curIndex < sumCount - 1) { //下滾動(dòng) moveNext(); } } else if (e.detail) {//Firefox t = e.detail; if (t < 0 && curIndex > 0) { //上滾動(dòng) movePrev(); } else if (t > 0 && curIndex < sumCount - 1) { //下滾動(dòng) moveNext(); } } }; function moveNext(){ container.css("transform", "translate3D(0, -" + (++curIndex) * $window.height() + "px, 0)"); } function movePrev(){ container.css("transform", "translate3D(0, -" + (--curIndex) * $window.height() + "px, 0)"); } function init(){ /*注冊(cè)事件*/ if (document.addEventListener) { document.addEventListener('DOMMouseScroll', scrollFunc, false); }//W3C window.onmousewheel = document.onmousewheel = scrollFunc;//IE/Opera/Chrome //設(shè)置動(dòng)畫 container.css({ "transition": "all 0.5s", "-moz-transition": "all 0.5s", "-webkit-transition": "all 0.5s" }); } </script>

    為了防止頁面在滾動(dòng)的時(shí)候用戶繼續(xù)滾動(dòng)打亂節(jié)奏,可以用時(shí)間來強(qiáng)制控制,即在滾動(dòng)期間不允許調(diào)用moveNext和movePrev函數(shù),最終代碼如下:

    <!DOCTYPE html><html><head lang="ch">    <meta charset="UTF-8">    <meta name=”viewport” content="width=device-width, user-scalable=no, initial-scale=1.0">    <title></title>    <style> body, html{ padding: 0; margin: 0; } body, html { height: 100%; overflow: hidden; } #container, .section { height: 100%; } .section { background-color: #000; background-size: cover; background-position: 50% 50%; } #section0 { background-color: #83af9b; } #section1 { background-color: #764d39; } #section2 { background-color: #ff9000; } #section3 { background-color: #380d31; } </style></head><body><div id="container">    <div class="section" id="section0"></div>    <div class="section" id="section1"></div>    <div class="section" id="section2"></div>    <div class="section" id="section3"></div></div><script src="http://code.jquery.com/jquery-latest.js"></script><script> var curIndex = 0; var container = $("#container"); var sumCount = $(".section").length; var $window = $(window); var duration = 500; //時(shí)間控制 var aniTime = 0; var scrollFunc = function (e) { //如果動(dòng)畫還沒執(zhí)行完,則return if(new Date().getTime() < aniTime + duration){ return; } e = e || window.event; var t = 0; if (e.wheelDelta) {//IE/Opera/Chrome t = e.wheelDelta; if (t > 0 && curIndex > 0) { //上滾動(dòng) movePrev(); } else if (t < 0 && curIndex < sumCount - 1) { //下滾動(dòng) moveNext(); } } else if (e.detail) {//Firefox t = e.detail; if (t < 0 && curIndex > 0) { //上滾動(dòng) movePrev(); } else if (t > 0 && curIndex < sumCount - 1) { //下滾動(dòng) moveNext(); } } }; function moveNext(){ //獲取動(dòng)畫開始時(shí)的時(shí)間 aniTime = new Date().getTime(); container.css("transform", "translate3D(0, -" + (++curIndex) * $window.height() + "px, 0)"); } function movePrev(){ //獲取動(dòng)畫開始時(shí)的時(shí)間 aniTime = new Date().getTime(); container.css("transform", "translate3D(0, -" + (--curIndex) * $window.height() + "px, 0)"); } function init(){ /*注冊(cè)事件*/ if (document.addEventListener) { document.addEventListener('DOMMouseScroll', scrollFunc, false); }//W3C window.onmousewheel = document.onmousewheel = scrollFunc;//IE/Opera/Chrome container.css({ "transition": "all 0.5s", "-moz-transition": "all 0.5s", "-webkit-transition": "all 0.5s" }); } init(); </script></body></html>

    版權(quán)聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載。

    本網(wǎng)站聲明
    本文內(nèi)容由網(wǎng)友自願(yuàn)投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請(qǐng)聯(lián)絡(luò)admin@php.cn

    熱AI工具

    Undress AI Tool

    Undress AI Tool

    免費(fèi)脫衣圖片

    Undresser.AI Undress

    Undresser.AI Undress

    人工智慧驅(qū)動(dòng)的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

    AI Clothes Remover

    AI Clothes Remover

    用於從照片中去除衣服的線上人工智慧工具。

    Clothoff.io

    Clothoff.io

    AI脫衣器

    Video Face Swap

    Video Face Swap

    使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!

    熱工具

    記事本++7.3.1

    記事本++7.3.1

    好用且免費(fèi)的程式碼編輯器

    SublimeText3漢化版

    SublimeText3漢化版

    中文版,非常好用

    禪工作室 13.0.1

    禪工作室 13.0.1

    強(qiáng)大的PHP整合開發(fā)環(huán)境

    Dreamweaver CS6

    Dreamweaver CS6

    視覺化網(wǎng)頁開發(fā)工具

    SublimeText3 Mac版

    SublimeText3 Mac版

    神級(jí)程式碼編輯軟體(SublimeText3)

    我如何了解最新的HTML標(biāo)準(zhǔn)和最佳實(shí)踐? 我如何了解最新的HTML標(biāo)準(zhǔn)和最佳實(shí)踐? Jun 20, 2025 am 08:33 AM

    要跟上HTML標(biāo)準(zhǔn)和最佳實(shí)踐,關(guān)鍵在於有意為之而非盲目追隨。首先,關(guān)注官方來源如WHATWG和W3C的摘要或更新日誌,了解新標(biāo)籤(如)和屬性,將其作為參考解決疑難問題;其次,訂閱可信的網(wǎng)頁開發(fā)新聞通訊和博客,每週花10-15分鐘瀏覽更新,關(guān)注實(shí)際用例而非僅收藏文章;再次,使用開發(fā)者工具和linters如HTMLHint,通過即時(shí)反饋優(yōu)化代碼結(jié)構(gòu);最後,與開發(fā)者社區(qū)互動(dòng),分享經(jīng)驗(yàn)並學(xué)習(xí)他人實(shí)戰(zhàn)技巧,從而持續(xù)提升HTML技能。

    如何使用元素來表示文檔的主要內(nèi)容? 如何使用元素來表示文檔的主要內(nèi)容? Jun 19, 2025 pm 11:09 PM

    使用標(biāo)籤的原因是提升網(wǎng)頁的語義化結(jié)構(gòu)和可訪問性,使屏幕閱讀器和搜索引擎更易理解頁面內(nèi)容,並允許用戶快速跳轉(zhuǎn)至核心內(nèi)容。以下是關(guān)鍵要點(diǎn):1.每個(gè)頁面應(yīng)僅包含一個(gè)元素;2.不應(yīng)包括跨頁面重複的內(nèi)容(如側(cè)邊欄或頁腳);3.可與ARIA屬性結(jié)合使用以增強(qiáng)無障礙體驗(yàn)。通常位於和之後、之前,用於包裹唯一的頁面內(nèi)容,例如文章、表單或產(chǎn)品詳情,並應(yīng)避免嵌套在、或中;為提高輔助功能,可使用aria-labelledby或aria-label明確標(biāo)識(shí)部分。

    如何創(chuàng)建基本的HTML文檔? 如何創(chuàng)建基本的HTML文檔? Jun 19, 2025 pm 11:01 PM

    要?jiǎng)?chuàng)建一個(gè)基本的HTML文檔,首先需要了解其基本結(jié)構(gòu)並按照標(biāo)準(zhǔn)格式編寫代碼。 1.開始時(shí)使用聲明文檔類型;2.使用標(biāo)籤包裹整個(gè)內(nèi)容;3.在其中包含和兩個(gè)主要部分,用於存放元數(shù)據(jù)如標(biāo)題、樣式錶鍊接等,而則包含用戶可見的內(nèi)容如標(biāo)題、段落、圖片和鏈接;4.保存文件為.html格式並在瀏覽器中打開查看效果;5.隨後可逐步添加更多元素以豐富頁面內(nèi)容。遵循這些步驟即可快速構(gòu)建一個(gè)基礎(chǔ)網(wǎng)頁。

    如何使用 如何使用 Jun 19, 2025 pm 11:41 PM

    要?jiǎng)?chuàng)建HTML複選框,需使用type屬性設(shè)為checkbox的元素。 1.基本結(jié)構(gòu)包含id、name和label標(biāo)籤,確保點(diǎn)擊文字可切換選項(xiàng);2.多個(gè)相關(guān)複選框應(yīng)使用相同name但不同value,並用fieldset包裹提升可訪問性;3.自定義樣式時(shí)隱藏原生控件並用CSS設(shè)計(jì)替代元素,同時(shí)保持功能完整;4.確??捎眯?,配對(duì)label、支持鍵盤導(dǎo)航且避免僅依賴視覺提示。以上步驟能幫助開發(fā)者正確實(shí)現(xiàn)兼具功能與美觀的複選框組件。

    如何使用元素代表文檔或部分的頁腳? 如何使用元素代表文檔或部分的頁腳? Jun 25, 2025 am 12:57 AM

    是HTML5中用於定義頁面或內(nèi)容區(qū)塊底部的語義化標(biāo)籤,通常包含版權(quán)信息、聯(lián)繫方式或?qū)Ш芥溄拥龋凰芍渺俄撁娴撞炕蚯短自?、等?biāo)籤內(nèi)作為區(qū)塊尾部;使用時(shí)應(yīng)注意避免重複濫用及放入無關(guān)內(nèi)容。

    隨著時(shí)間的流逝,HTML如何發(fā)展,其歷史上的關(guān)鍵里程碑是什麼? 隨著時(shí)間的流逝,HTML如何發(fā)展,其歷史上的關(guān)鍵里程碑是什麼? Jun 24, 2025 am 12:54 AM

    htmlhasevolvedscreatscreationtomeetthegrowingdemandsofwebdevelopersandusers.inatelyallyasimplemarkuplanguageforsharingdocuments,ithasundergonemajorupdates,包括html.2.0,包括wheintrodistusefforms;

    如何使用Tabindex屬性來控制元素的選項(xiàng)卡順序? 如何使用Tabindex屬性來控制元素的選項(xiàng)卡順序? Jun 24, 2025 am 12:56 AM

    ThetabindexattributecontrolshowelementsreceivefocusviatheTabkey,withthreemainvalues:tabindex="0"addsanelementtothenaturaltaborder,tabindex="-1"allowsprogrammaticfocusonly,andtabindex="n"(positivenumber)setsacustomtabbing

    See all articles