[英]jQuery Fix page scrolling to top after click
Im just building a basic lightbox type plugin to suit the needs of a project im building. 我只是构建一个基本的灯箱类型插件来满足我所构建项目的需要。 It basically just allows the user to click on an image on the page and the image appear in the lightbox, the problem is when i click on the close button on the lightbox,the script causes the window to shoots to the top of the page. 它基本上只允许用户单击页面上的图像并且该图像出现在灯箱中,问题是当我单击灯箱上的关闭按钮时,脚本使窗口拍摄到页面顶部。
This is unwanted behavior because if i have been scrolling on 1000+ pictures for example i wouldnt want it to just undo all that scroll just by closing a picture. 这是不想要的行为,因为例如,如果我一直在滚动1000幅以上的图片,则我不希望它仅通过关闭图片即可撤消所有滚动。
Here is my fiddle: https://jsfiddle.net/w407wdrv/ 这是我的小提琴: https : //jsfiddle.net/w407wdrv/
Here is my code: 这是我的代码:
<style>
body, html {
padding: 0;
border: 0;
margin: 0;
}
.flavius-modal-bg {
display: none;
position: fixed;
background: rgba(0,0,0,0.8);
width: 100%;
height: 100%;
z-index: 1;
}
.flavius-img-section {
background: rgba(255,255,255,0.8);
height: 80%;
width: 80%;
margin: 0 auto;
text-align: center;
z-index: 100;
}
.flavius-options-section {
background: white;
height: 20%;
width: 80%;
margin: 0 auto;
}
.flavius-img-preview {
position: relative;
top: 50%;
transform: translateY(-50%);
min-width: 500px;
width: 500px;
max-height: 100%;
}
</style>
<div class="flavius-modal-bg">
<div class="flavius-img-section">
<img class="flavius-img-preview" src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcQvWoePDuMwoKkA2f6LIIgWg7nlR5wq5pJwM8DJucMvlEF94wEV" alt="">
</div>
<div class="flavius-options-section">
<a class="close-modal-bg" href="#">Close Me</a>
<a href="#">Some button clicked</a>
</div>
</div>
<img src="http://s1.1zoom.me/b5050/644/Ferrari_Formula_1_f1_448686_1334x750.jpg" alt="">
<img src="https://insanelyi.com/uploads/gallery/album_102/gallery_1_102_91150.jpg" alt="">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$('img').on('click', function(){
var currentImg = $(this).attr('src');
$('img.flavius-img-preview').attr('src', currentImg);
$('.flavius-modal-bg').css('display', 'block');
});
$('.close-modal-bg').on('click', function(){
$scrollTop = $('body').scrollTop();
$('.flavius-modal-bg').css('display', 'none');
setScrollPosition($scrollTop);
});
function setScrollPosition($scrollTop)
{
$('body').scrollTop($scrollTop);
}
</script>
You can use javascript:void(0);
您可以使用javascript:void(0);
to achieve your req. 达到您的要求。
Please find below working snippet 请在下面找到工作片段
$('img').on('click', function(){ var currentImg = $(this).attr('src'); $('img.flavius-img-preview').attr('src', currentImg); $('.flavius-modal-bg').css('display', 'block'); }); $('.close-modal-bg').on('click', function(){ $scrollTop = $('body').scrollTop(); $('.flavius-modal-bg').css('display', 'none'); setScrollPosition($scrollTop); }); function setScrollPosition($scrollTop) { $('body').scrollTop($scrollTop); }
body, html { padding: 0; border: 0; margin: 0; } .flavius-modal-bg { display: none; position: fixed; background: rgba(0,0,0,0.8); width: 100%; height: 100%; z-index: 1; } .flavius-img-section { background: rgba(255,255,255,0.8); height: 80%; width: 80%; margin: 0 auto; text-align: center; z-index: 100; } .flavius-options-section { background: white; height: 20%; width: 80%; margin: 0 auto; } .flavius-img-preview { position: relative; top: 50%; transform: translateY(-50%); min-width: 500px; width: 500px; max-height: 100%; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <div class="flavius-modal-bg"> <div class="flavius-img-section"> <img class="flavius-img-preview" src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcQvWoePDuMwoKkA2f6LIIgWg7nlR5wq5pJwM8DJucMvlEF94wEV" alt=""> </div> <div class="flavius-options-section"> <a class="close-modal-bg" href="javascript:void(0);">Close Me</a> <a href="#">Some button clicked</a> </div> </div> <img src="http://s1.1zoom.me/b5050/644/Ferrari_Formula_1_f1_448686_1334x750.jpg" alt=""> <img src="https://insanelyi.com/uploads/gallery/album_102/gallery_1_102_91150.jpg" alt="">
I update your jsfiddle here https://jsfiddle.net/w407wdrv/1/ The problem is on <a class="close-modal-bg" href="#">Close Me</a>
, by replacing href="#"
with href="javascript:void(0);"
我在这里更新您的jsfiddle https://jsfiddle.net/w407wdrv/1/问题出现在<a class="close-modal-bg" href="#">Close Me</a>
,方法是替换href="#"
与href="javascript:void(0);"
it will prevent the page to scroll on top. 它将阻止页面在顶部滚动。
The problem is just the ash that you use for the href: 问题只是您用于href的灰烬:
<div class="flavius-options-section">
<a class="close-modal-bg" href="#">Close Me</a>
<a href="#">Some button clicked</a>
</div>
1.Remove the ash from the href of the closing anchor 1.从关闭锚点的href移除灰烬
<div class="flavius-options-section">
<a class="close-modal-bg">Close Me</a>
<a href="#">Some button clicked</a>
</div>
2.Add a class to style the link 2.添加一个类来设置链接样式
.close-modal-bg{
cursor:pointer;
text-decoration: underline;
color:blue;
}
$('img').on('click', function(){ var currentImg = $(this).attr('src'); $('img.flavius-img-preview').attr('src', currentImg); $('.flavius-modal-bg').css('display', 'block'); }); $('.close-modal-bg').on('click', function(){ $scrollTop = $('body').scrollTop(); $('.flavius-modal-bg').css('display', 'none'); setScrollPosition($scrollTop); }); function setScrollPosition($scrollTop) { $('body').scrollTop($scrollTop); }
body, html { padding: 0; border: 0; margin: 0; } .flavius-modal-bg { display: none; position: fixed; background: rgba(0,0,0,0.8); width: 100%; height: 100%; z-index: 1; } .flavius-img-section { background: rgba(255,255,255,0.8); height: 80%; width: 80%; margin: 0 auto; text-align: center; z-index: 100; } .flavius-options-section { background: white; height: 20%; width: 80%; margin: 0 auto; } .flavius-img-preview { position: relative; top: 50%; transform: translateY(-50%); min-width: 500px; width: 500px; max-height: 100%; } .close-modal-bg{ cursor:pointer; text-decoration: underline; color: blue; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="flavius-modal-bg"> <div class="flavius-img-section"> <img class="flavius-img-preview" src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcQvWoePDuMwoKkA2f6LIIgWg7nlR5wq5pJwM8DJucMvlEF94wEV" alt=""> </div> <div class="flavius-options-section"> <a class="close-modal-bg">Close Me</a> <a href="#">Some button clicked</a> </div> </div> <img src="http://s1.1zoom.me/b5050/644/Ferrari_Formula_1_f1_448686_1334x750.jpg" alt=""> <img src="https://insanelyi.com/uploads/gallery/album_102/gallery_1_102_91150.jpg" alt="">
change your clsoe function to this, 将您的clsoe函数更改为此,
$('.close-modal-bg').on('click', function(e){
e.preventDefault();
$scrollTop = $('body').scrollTop();
$('.flavius-modal-bg').css('display', 'none');
setScrollPosition($scrollTop);
});
As in w3schools.com , 与在w3schools.com中一样 ,
The event.preventDefault() method stops the default action of an element from happening. event.preventDefault()方法停止发生元素的默认操作。
For example: 例如:
1.Prevent a submit button from submitting a form 1.防止提交按钮提交表单
2.Prevent a link from following the URL 2.防止链接跟随URL
Hope this helps you. 希望这对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.