html:
show div
hide div
function preventScroll(e){
//document.body.scrollTop=0;
e.preventDefault();
}
document.getElementById("showDivandFix").addEventListener("click",function(){
if(!document.getElementById("myDiv")){
var myDiv = document.createElement("div");
myDiv.id="myDiv";
myDiv.style.width="100px";
myDiv.style.height="100px";
myDiv.style.backgroundColor='#00ee00';
document.getElementById("myDivContainer").appendChild(myDiv);
}
document.getElementById("myDiv").style.display="block";
document.body.addEventListener("mousewheel",preventScroll,false);
},false);
document.getElementById("hideDivandremoveFix").addEventListener("click",function(){
document.getElementById("myDiv").style.display="none";
document.body.removeEventListener("mousewheel",preventScroll,false);
},false);
或者
jquery:
$(function(){
function preventScroll(e){
//document.body.scrollTop=0;
e.preventDefault();
}
$('button#showDivandFix').on("click",function(){
if($('#myDiv').length==0){
$('#myDivContainer').append("
}
$('#myDiv').show();
$('body').on("mousewheel",preventScroll);
});
$('button#hideDivandremoveFix').on("click",function(){
$('#myDiv').hide();
$('body').off("mousewheel",preventScroll);
});
});