【发布时间】:2019-03-20 02:16:56
【问题描述】:
我正在尝试为我的个人网站上的某些证书实现仅 CSS 的灯箱效果。不幸的是,每当我单击要展开的图像时,它要么滚动到页面顶部,要么向下滚动一点-而不是针对全尺寸图像(我相信这是由于平滑滚动逻辑令人困惑图像目标中的哈希标记与页面目标中的哈希标记)。我只能通过在浏览器中手动输入地址来访问展开的图像。有什么建议吗??
<div class="background-certs" id="certificates">
<div class="title-box">
<h3 class="h3-web">{ certificates }</h3>
<h4 class="h4-web">I enjoy learning</h4>
</div>
<div class="cert-container">
<a href="#cert-item-1" class="button-new">
<div class="box1 grid-sub">
<%= image_tag "firehose.jpg", class: "full-width-cert" %>
</div>
</a>
<h4 class="h4-subtitle">software engineering bootcamp</h4>
<p class="cert-subtitle">Dec. 2017 - Oct. 2018</p>
</div>
<div class="cert-container">
<a href="#cert-item-2" class="button-new">
<div class="box1 grid-sub">
<%= image_tag "udemy-js.jpg", class: "full-width-cert" %>
</div>
</a>
<h4 class="h4-subtitle">JavaScript: the weird parts</h4>
<p class="cert-subtitle">Nov. 2018</p>
</div>
<br class="clear" />
<div class="cert-container">
<a href="#cert-item-3" class="button-new">
<div class="box1 grid-sub">
<%= image_tag "udemy-algos.jpg", class: "full-width-cert" %>
</div>
</a>
<h4 class="h4-subtitle">algorithms + data structures</h4>
<p class="cert-subtitle">Dec. 2018</p>
</div>
<div class="cert-container">
<a href="#cert-item-4" class="button-new">
<div class="box1 grid-sub">
<%= image_tag "udemy-shopify.jpg", class: "full-width-cert" %>
</div>
</a>
<h4 class="h4-subtitle">shopify themes from scratch</h4>
<p class="cert-subtitle">Feb. 2019</p>
</div>
<div class="certificate-lightboxes">
<div class="cert-lightbox" id="cert-item-1">
<div class="cert-lightbox__content">
<a href="#certificates" class="close"></a>
<%= image_tag "firehose.jpg", class: "full-width-cert" %>
</div>
</div>
<div class="cert-lightbox" id="cert-item-2">
<div class="cert-lightbox__content">
<a href="#certificates" class="close"></a>
<%= image_tag "udemy-js.jpg", class: "full-width-cert" %>
</div>
</div>
<div class="cert-lightbox" id="cert-item-3">
<div class="cert-lightbox__content">
<a href="#certificates" class="close"></a>
<%= image_tag "udemy-algos.jpg", class: "full-width-cert" %>
</div>
</div>
<div class="cert-lightbox" id="cert-item-4">
<div class="cert-lightbox__content">
<a href="#certificates" class="close"></a>
<%= image_tag "udemy-shopify.jpg", class: "full-width-cert" %>
</div>
</div>
</div>
<div class="bottom-line"></div>
</div>
/** LIGHTBOX MARKUP **/
.cert-lightbox {
display: flex;
position: fixed;
width: 100%;
height: 100%;
top: 0;
left: 0;
background: black;
align-items: center;
justify-content: center;
transform: scale(0, 0);
transition: transform ease-in-out 100ms;
}
.cert-lightbox:target {
transform: scale(1, 1) !important;
display: flex;
z-index: 900;
}
.cert-lightbox__content {
width: 65%;
position: relative;
}
.close {
position: absolute;
width: 2em;
height: 2em;
background: red;
top: -1em;
right: -1em;
border-radius: 50%;
text-decoration: none;
display: flex;
align-items: center;
justify-content: center;
}
.close::after {
content: "X";
color: white;
font-weight: 700;
}
在 application.html.erb 中平滑滚动
<script type="text/javascript">
var $root = $('html, body');
$('a[href^="#"]').click(function () {
$root.animate({
scrollTop: $( $.attr(this, 'href') ).offset().top
}, 400);
return false;
});
</script>
scroll.js 中的平滑滚动
function scrollToHash(event) {
// On-page links
if (
location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '')
&&
location.hostname == this.hostname
) {
// Figure out element to scroll to
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
// Does a scroll target exist?
if (target.length) {
// Only prevent default if animation is actually gonna happen
event.preventDefault();
$('html, body').animate({
scrollTop: target.offset().top
}, 1000, function() {
// Callback after animation
// Must change focus!
var $target = $(target);
$target.focus();
if ($target.is(":focus")) { // Checking if the target was focused
return false;
} else {
$target.attr('tabindex','-1'); // Adding tabindex for elements not focusable
$target.focus(); // Set focus again
};
});
}
}
};
// Select all links with hashes
$('a[href*="#"]')
// Remove links that don't actually link to anything
.not('[href="#"]')
.not('[href="#0"]')
.click(scrollToHash);
【问题讨论】:
-
平滑滚动从何而来?在您发布的代码中,我没有看到任何与平滑滚动相关的内容。在您链接的网站上,页面的该部分没有灯箱,也没有平滑滚动(在 macOS 上的 Firefox 中),所以我也无法测试它。
-
@RoryO'Kane 平滑滚动设置为向上或向下滚动到带有井号的目标。由于灯箱的目标是#cert-item-1、#cert-item-2 等,我认为由于某种原因,当我单击图像以“打开”灯箱时,它会激活滚动效果。我只链接了我的网站,因此您可以通过 Chrome DevTools 访问主页上向下箭头上滚动的 JS 代码,以及联系页面上的返回顶部箭头。我继续将更改实时推送到我的网站,因此您现在应该能够访问灯箱代码。感谢您查看!
-
该描述有帮助。我认为解决方案是编辑平滑滚动或灯箱库的设置方式——或者向 JavaScript 添加配置以设置它们以仅强制灯箱处理这些链接,或者将库切换到更可配置的库。如果您需要这方面的帮助,请编辑您的问题以包含初始化这些库的 JavaScript。确保清楚您正在使用哪些库。
-
@RoryO'Kane 谢谢,问题已被编辑。所以我尝试编辑 scrollToHash 函数以忽略证书目标而没有运气。我在 .not('[href="#"]') 下的函数底部添加了额外的行,使用 .not('[href="#cert-item-1"]') 等等。我会继续四处寻找解决方案,如果您有任何建议,我会全力以赴!
-
您没有使用文档类型,而是处于“怪癖模式”,至少从 1999 年起就没有人应该这样做了。在您正确插入一个可能会改变您所做的所有工作的文档类型之前,您将继续遇到问题。