2016-08-22 155 views
2

當iframe元素隱藏並再次顯示時,iframe元素上的滾動條未顯示。iframe滾動條在隱藏後不顯示並顯示iframe元素

代碼片斷:

<iframe id="imageFrame" scrolling="yes" src="https://wallpaperscraft.com/image/the_legend_of_zelda_elf_shield_sky_link_22301_1280x1024.jpg" width="100" height="100"> 

</iframe> 
<button id="hideFrame"> 
Hide Frame 
</button> 
<button id="showFrame"> 
Show Frame 
</button> 

$(document).ready(function() { 
    $("#hideFrame").click(function() { 
    $("#imageFrame").hide(); 
    }); 

    $("#showFrame").click(function() { 
    $("#imageFrame").show(); 
    }); 
}); 

的jsfiddle DEMO: DEMO

回答

2

解決方案1:

HTML:

<iframe id="imageFrame" scrolling="yes" src="https://wallpaperscraft.com/image/the_legend_of_zelda_elf_shield_sky_link_22301_1280x1024.jpg" width="100" height="100"> 
</iframe> 
<button id="hideFrame"> 
    Hide Frame 
</button> 
<button id="showFrame"> 
    Show Frame 
</button> 

CSS:

.hide{visibility: hidden;position: absolute;} 
.show{visibility: visible;} 

的jQuery:

$(document).ready(function() { 
    $("#hideFrame").click(function() { 
    $("#imageFrame").addClass('hide').removeClass('show'); 
    }); 

    $("#showFrame").click(function() { 
    $("#imageFrame").addClass('show').removeClass('hide'); 
    }); 
}); 

演示:https://jsfiddle.net/17knnLsb/7/


解決方案2:

HTML:

<div class="iframe-wrapper"></div> 
<button id="hideFrame"> 
    Hide Frame 
</button> 
<button id="showFrame"> 
    Show Frame 
</button> 

的jQuery:

var iframeMarkup = '<iframe id="imageFrame" scrolling="yes" src="https://wallpaperscraft.com/image/the_legend_of_zelda_elf_shield_sky_link_22301_1280x1024.jpg" width="100" height="100"></iframe>'; 

$(document).ready(function() { 
    $('.iframe-wrapper').append(iframeMarkup); 
    $("#hideFrame").click(function() { 
    $('.iframe-wrapper').find('iframe').remove(); 
    }); 

    $("#showFrame").click(function() { 
    $('.iframe-wrapper').append(iframeMarkup); 
    }); 
}); 

演示:https://jsfiddle.net/17knnLsb/3/

+0

這裏的問題是,如果是iframe中有什麼變化 - 變化將不會生效的顯示/隱藏後,因爲你重新加載它。 – Dekel

+0

@Dekel這個怎麼樣? https://jsfiddle.net/17knnLsb/5/ –

+0

我覺得這樣好多了。請注意,您並不需要'z-index'和'opacity'。 – Dekel