简体   繁体   English

获取 position:固定侧边栏在页脚处停止(并将 JS 添加到 Wordpress)

[英]Getting a position:fixed sidebar to stop at footer (and adding JS to Wordpress)

Apologies if this seems simple, but I am really struggling here and new to JS so not sure if I am just missing something!!!抱歉,如果这看起来很简单,但是我在这里真的很挣扎并且对 JS 很陌生,所以不确定我是否只是遗漏了一些东西!!!

Page: https://www.villaslegianbali.com/reviews/页码: https://www.villaslegianbali.com/reviews/

I have added the sidebar to allow people to find the reviews they want.我添加了侧边栏,让人们可以找到他们想要的评论。 However, it appears over the footer.但是,它出现在页脚上。 I tried changing the z-index (sidebar to 0, footer to 99) for now, but even that isn't working.我现在尝试更改z-index (侧边栏为 0,页脚为 99),但即使这样也不起作用。

When I try to save the below JS, it comes up with an error:当我尝试保存以下 JS 时,出现错误:

"Your PHP code changes were rolled back due to an error on line 325 of file wp-content/themes/villalegianbali/functions.php. Please fix and try saving again. “由于文件 wp-content/themes/villalegianbali/functions.php 的第 325 行出现错误,您的 PHP 代码更改已回滚。请修复并再次尝试保存。

syntax error, unexpected 'var' (T_VAR), expecting end of file"语法错误,意外的 'var' (T_VAR),期待文件结尾"
(line 325 is the first line of the JS above) (第325行是上面JS的第一行)

Code:代码:

 //JS I added to the functions.php file: var sideNav = document.querySelector('.sidenav'); var footer = document.querySelector('.footer'); function checkOffset() { function getRectTop(el) { var rect = el.getBoundingClientRect(); return rect.top; } if ((getRectTop(sideNav) + document.body.scrollTop) + sideNav.offsetHeight >= (getRectTop(footer) + document.body.scrollTop) - 10) sideNav.style.position = 'absolute'; if (document.body.scrollTop + window.innerHeight < (getRectTop(footer) + document.body.scrollTop)) sideNav.style.position = 'fixed'; // restore when you scroll up sideNav.innerHTML = document.body.scrollTop + window.innerHeight; } document.addEventListener("scroll", function() { checkOffset(); });
 .footer { height: 243px; z-index: 999.important }:sidenav { width; 180px: position; fixed: bottom; 0: left; 30px: background-color; #fff: overflow; hidden: padding; 10px; }
 <div class="sidenav-parent"> <div class="sidenav reviews-menu" style="position: fixed; float: left; bottom:0; margin-left: -30px; margin-bottom: 15px"> <h4 class="reviews-menu-title">Choose your Villa:</h4> <a href="#karma" margin="0"> <h3 class="reviews-menu-item">Villa Karma Legian</h3> </a> <a href="#poppy" margin="0"> <h3 class="reviews-menu-item">Villa Poppy Legian</h3> </a> <a href="#aniela" margin="0"> <h3 class="reviews-menu-item">Villa Aniela</h3> </a> <a href="#segara" margin="0"> <h3 class="reviews-menu-item">Villa Segara Legian</h3> </a> <a href="#tropical" margin="0"> <h3 class="reviews-menu-item">Tropical House</h3> </a> <a href="#zakira" margin="0"> <h3 class="reviews-menu-item">Villa Zakira Legian</h3> </a> </div> </div> (content)

Please put below CSS请放在下面 CSS

@media screen and (min-width: 980px) {
  .footer {
        z-index: 99 !important;
        position: absolute;
    }
}

We can't directly put Javascript in the functions.php file.我们不能直接将 Javascript 放在 functions.php 文件中。 To add JS first save in the.js file and include it in the functions.php file.添加 JS 首先保存在 .js 文件中,并将其包含在 functions.php 文件中。

For example. Follow below steps
1] Create a .js file as name scrollToTop.js
2] Put the below code in that JS file

var sideNav = document.querySelector('.sidenav');
var footer = document.querySelector('.footer');

function checkOffset() {
  function getRectTop(el){
    var rect = el.getBoundingClientRect();
    return rect.top;
  }
  
  if((getRectTop(sideNav) + document.body.scrollTop) + sideNav.offsetHeight >= (getRectTop(footer) + document.body.scrollTop) - 10)
    sideNav.style.position = 'absolute';
  if(document.body.scrollTop + window.innerHeight < (getRectTop(footer) + document.body.scrollTop))
    sideNav.style.position = 'fixed'; // restore when you scroll up
  
  sideNav.innerHTML = document.body.scrollTop + window.innerHeight;
}

document.addEventListener("scroll", function(){
  checkOffset();
});

3] Put the below code in the functions.php file and check. 3]将以下代码放入functions.php文件并检查。

function villas_adding_scripts_for_scroll() {
    wp_register_script('villas_scrolltop_script', get_template_directory_uri() . '/scrollToTop.js', array('jquery'),'1.1', true);
    wp_enqueue_script('villas_scrolltop_script');
} 

add_action( 'wp_enqueue_scripts', 'villas_adding_scripts_for_scroll', 999 ); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM