我正在尝试找出一种方法来检测横幅内特定跨度标签中的自动换行。如果它换行到 2 行,则将容器的整体高度增加 56 像素。还有一个子标题(headline2)也需要增加(或减少)高度40px。
我在这里写了一些基本的 JS 代码,它检查跨度的 div 高度,但它不是很好,也只能用于 3 行。
// Variable banner heights
var hl11sub = 368;
var hl21sub = 448;
var hl31sub = 548;
var hl12sub = 416;
var hl22sub = 496;
var hl32sub = 576;
var hLFontSizeCSS = window.getComputedStyle(headlineText, null).getPropertyValue("font-size");
var hL2FontSizeCSS = window.getComputedStyle(headline2text, null).getPropertyValue("font-size");
var bannerHeightCSS = window.getComputedStyle(banner, null).getPropertyValue("height");
var headlineHeight = headlineText.offsetHeight;
var hL2HeadHeight = headline2text.offsetHeight;
var headHeight = headlineText.style.lineHeight = parseInt(hLFontSizeCSS) + 10 + "px";
var hL2Height = headline2text.style.lineHeight = parseInt(hL2FontSizeCSS) + …Run Code Online (Sandbox Code Playgroud) 我正在构建一个简单的单页网站并使用 JQuery 更改页面上导航链接的颜色。我已经实现了平滑滚动,可以很好地到达每个锚点。我还实现了一项功能,允许用户向下滚动页面,并将活动类应用于导航中的锚点以更改颜色。
一切工作几乎正确......除了:当您单击导航链接时,活动类未应用于正确的链接。
到目前为止,我已经尝试改变类的应用方式以及直接 ID 的名称,但它的表现仍然不完美。显然发生了一些不正确的事情$('.nav li').eq(i).find('a').addClass('active');
谁能建议我如何解决这个问题?
这是带 html 的 CP: https://codepen.io/nolimit966/pen/aboqvOP
谢谢
JS:
$(window).on('scroll',function(){
var WindowTop = $(window).scrollTop();
$('section').each(function(i){
if(WindowTop > $(this).offset().top - 50 &&
WindowTop < $(this).offset().top + $(this).outerHeight(true)
){
$('.nav > li > a').removeClass('active');
$('.nav li').eq(i).find('a').addClass('active');
}
});
});
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top
}, 1000);
return false; …Run Code Online (Sandbox Code Playgroud)