国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > WEB開發 > 正文

Angular封裝指令回到頂部以及滾動到特定的頁面位置

2024-04-27 15:11:25
字體:
來源:轉載
供稿:網友

主要是使用angular的指令系統來自己封裝一個指令。其寫法有多種,這里只是很簡單的一種寫法。

HTML片段代碼1:

 <div class="text-center bigtop">                <go-you-want>                    <img ng-src="{{downimg}}"/>                </go-you-want>   </div>

HTML片段代碼2:

 <div class="goTop clearfix" style="position: fixed;right: 10px;bottom: 200px;">            <back-to-top class="label " title="返回頂部"><img src="img/gotop_03.png"/></back-to-top> </div>

js指令:

myApp.directive("goYouWant", function() {    return {        restrict : "E",        link : function(scope, element, attr) {            var e = $(element);            var firstDivHeight = $('.firstH').outerHeight();  //這里需要自己定義需要到達的地方            e.click(function() {                $('body, html').animate({                    "scrollTop" : firstDivHeight                }, 500)            })        }    }});

//回到頂部//var flag = false;myApp.directive("backToTop", function () {    return {        restrict: "E",        link: function (scope, element, attr) {            var e = $(element);            if($(document).scrollTop() == 0) {                e.fadeOut(200);            }            $(window).scroll(function () {                 //滾動時觸發                console.log($(document).scrollTop())                if ($(document).scrollTop() > 800) {                    //flag = true;                    e.fadeIn(300);                }    //獲取滾動條到頂部的垂直高度,到相對頂部800px高度顯示                else {                    //flag = false;                    e.fadeOut(200);                }            });            /*點擊回到頂部*/            e.click(function () {                $('html, body').animate({                 //添加animate動畫效果                    scrollTop: 0                }, 1000);            });        }    };});


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 晋城| 鹤岗市| 广州市| 彩票| 宝清县| 永川市| 中西区| 宜君县| 海口市| 滕州市| 阳原县| 富顺县| 郸城县| 尼勒克县| 宜兴市| 蕉岭县| 鹰潭市| 高要市| 延吉市| 华坪县| 赤水市| 永登县| 眉山市| 云南省| 昌都县| 绩溪县| 屯留县| 汤原县| 湟源县| 阳原县| 遂溪县| 汶川县| 肥乡县| 同江市| 武平县| 温宿县| 安平县| 临猗县| 昭觉县| 噶尔县| 邢台市|