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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

閉包的問題中執(zhí)行touchmove事件的問題

閉包的問題中執(zhí)行touchmove事件的問題

wubin_work 2019-05-28 11:12:28
要求點擊圖片出現(xiàn)彈窗 彈窗出現(xiàn)的時候,根據(jù)圖片高 > 屏幕高,來判斷是否阻止底層滾動,現(xiàn)在問題是,當我第一次點擊出現(xiàn)彈出層時候,圖片高度可以獲取到?判斷也沒問題?但是當我點擊一個長圖的時候,執(zhí)行touchmove事件時候,圖片高度出現(xiàn)了2種分別為第一次的彈窗圖片240 跟最新的圖片高度,我是在這里 執(zhí)行完彈出層后執(zhí)行回調(diào)函數(shù)getPopImgHeight,?我就不明白為什么再點擊的時候回有上次的變量 ,而且 應該再出現(xiàn)彈出層的時候 不應該重新獲取img的高度 吧上一個覆蓋了嗎?求大神幫忙解答下。。謝謝<div?id="popup-box">????<div?id="popclose"></div>????<div?class="imgbox"?id="imgbox">????????<img?src="">????</div>????<div?class="h6box?in"></div></div>var?popBox?=?$('#popup-box'),????popImg?=?popBox.find('img'),????popText?=?popBox.find('.h6box');var?screenH?=?$(window).height();function?getPopImgHeight()?{????var?imgHeight?=?popImg.height();????console.log('原來的'+imgHeight);????$('#imgbox').on('touchstart',?function()?{????????popText.attr('class',?'h6box?out');????});????$('#imgbox').on('touchmove',?function()?{????????console.log('imgHeight:'?+?imgHeight);????????console.log(imgHeight?<?screenH);????????if?(imgHeight?<?screenH)?{?//?阻止?jié)L動?????????????console.log('1')?????????????//?return?false;????????}?else?{?//?允許滾動?????????????console.log('2')?????????????//?return?true;????????}????});}$('.baoImgQ').on('click',?'a',?function()?{????stopDefault();????var?imgLen?=?$(this).find('img').length;????if?(imgLen?>?0)?{????????var?imgSrc?=?getRealSrc($(this).attr('href'));????????var?hText?=?$(this).parent().next().next('h6').text();????????console.log(imgSrc?+?';'?+?hText);????????addPopContent(imgSrc,?hText);????????popBox.fadeIn('fast',?getPopImgHeight);????}});//?關閉時候重置$('#popclose').on('click',?function()?{????popBox.fadeOut('fast');????popImg.attr('src',?'');????popText.text('').attr('class',?'h6box?in').show();});function getPopImgHeight() {? ? var imgHeight = popImg.height();? ? console.log('原來的'+imgHeight);? ? $('#imgbox').on('touchstart', function() {? ? ? ? popText.attr('class', 'h6box out');? ? });? ? $('#imgbox').on('touchmove', function() {? ? ? ? console.log('imgHeight:' + imgHeight);? ? ? ? console.log(imgHeight < screenH);? ? ? ? if (imgHeight < screenH) { // 阻止?jié)L動? ? ? ? ? ? ?console.log('1')? ? ? ? ? ? ?// return false;? ? ? ? } else { // 允許滾動? ? ? ? ? ? ?console.log('2')? ? ? ? ? ? ?// return true;? ? ? ? }? ? });}$('.baoImgQ').on('click', 'a', function() {? ? stopDefault();? ? var imgLen = $(this).find('img').length;? ? if (imgLen > 0) {? ? ? ? var imgSrc = getRealSrc($(this).attr('href'));? ? ? ? var hText = $(this).parent().next().next('h6').text();? ? ? ? console.log(imgSrc + ';' + hText);? ? ? ? addPopContent(imgSrc, hText);? ? ? ? popBox.fadeIn('fast', getPopImgHeight);? ? }});
查看完整描述

1 回答

已采納
?
pardon110

TA貢獻1038條經(jīng)驗 獲得超227個贊

在js中閉包通常用來存儲私有數(shù)據(jù),它是普通函數(shù)的一種擴展。簡單來說閉包函數(shù)引用了外層函數(shù)的局部就變量。正常情況下,函數(shù)調(diào)用完,函數(shù)內(nèi)的臨時局部變量在內(nèi)存中的存儲單元會被釋放。但調(diào)用閉包不會,它會保留上次執(zhí)行狀態(tài)。從某種意義上來講,閉包是有狀態(tài)的函數(shù)。示例如下

fn?=?function(){
????var?i?=?0
????return?function(){
????????i++
????????console.log(i)
????}
}
f?=?fn()
f()?//?1
f()?//?2
f()?//?3

fn2?=?function(){
????var?i?=?0
????console.log(i)
}
fn2(()???//?0
fn2()???//?0

換句話來說,你要注意js的函數(shù)嵌套,尤其是內(nèi)層函數(shù)使用了外層函數(shù)中的局部變量的情況。

查看完整回答
反對 回復 2019-05-28
  • 1 回答
  • 0 關注
  • 691 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號