第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

這段代碼怎么封裝好?

這段代碼怎么封裝好?

一只甜甜圈 2018-09-07 10:10:48
實(shí)現(xiàn)的功能是:1.頁面下拉時(shí)固定nav問題是兩個(gè)欄目后端是分離成組件并且公用的(smarty)!結(jié)構(gòu)和公用是很大的限制,還有代碼冗余,這段代碼如何分裝比較好,變化的參數(shù)寫進(jìn)smartFloat_nav();smartFloat_header();其中方法里面?
查看完整描述

1 回答

?
桃花長相依

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超8個(gè)贊

比較了一下你的代碼,兩段代碼只有這么一點(diǎn)不一樣

https://img1.sycdn.imooc.com//5baf12e70001b92d10250603.jpg

所以可以封裝成一個(gè)函數(shù),最多只是 smartFloat_nav 和 smartFloat_mall 調(diào)用不同。

共用部分不想暴露出來可以寫成閉包

(function() {

    function smartFloat(position1, position2) {

        var position = function(element) {

            var top = element.position().top;

            var left = element.position().left;

            pos = element.css("position");

            $(window).scroll(function() {

                var scrolls = $(this).scrollTop();

                if (scrolls >= top) {

                    if (window.XMLHttpRequest) {

                        element.css({

                            position: "fixed",

                            top: position1,

                            left: left

                        });

                    } else {

                        element.css({

                            top: scrolls,

                            left: left

                        });

                    }

                } else {

                    element.css({

                        position: "absolute",

                        top: position2,

                        left: 0,

                    });

                }

            });

        };

        return $(this).each(function() {

            position($(this));

        });

    }


    $.fn.smartFloat_mall = function() {

        smartFloat("0.81rem", "2.32rem");

    };

    $.fn.smartFloat_nav = function() {

        smartFloat("1.81rem", "2.32rem");

    };

})();


查看完整回答
反對(duì) 回復(fù) 2018-09-29
  • 1 回答
  • 0 關(guān)注
  • 694 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)