1

我知道这个问题还有其他解决方案,但我根本无法让它们工作

我有一个固定在底部的 div。我希望我在它碰到#footer 时停下来

网站

是 css 所以基本上我需要一个脚本,当它到达页脚的边界时,将类从 .widget.widget_nav_menu 更改为 .widget.widget_nav_menu.fixed_button

那可能吗?

.widget.widget_nav_menu{
  background: none repeat scroll 0 0 #2E3337;
  bottom: 0;
  padding: 25px;
  position: fixed;
  right: 2%;
  color: #707480;
    font-size: 0.8em;
    opacity: 1;
    text-transform: uppercase;
}
.widget.widget_nav_menu.fixed_button {
  margin-right: -210px;
  position: absolute !important;
  top: -180px;
  background: none repeat scroll 0 0 #2E3337;
  padding: 25px;
  right: 2%;
}
4

1 回答 1

4

StackOverflow 上有几个答案可以回答您的问题,如果 JavaScript 可以检测页面是否已触底:

借助上述内容,您只需更改 Class 即可:

<script>
$(function() {
    $(window).scroll(function(){

       //Cache the Footer Widget
       var $footerMenu = $('.widget.widget_nav_menu');

       //Check if it hits the bottom, toggle Relevant Class.
       if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight)
          $footerMenu.addClass('fixed_button');
       else
          $footerMenu.removeClass('fixed_button');

    });
});
</script>

演示小提琴:http: //jsfiddle.net/fVKZe/1/

要将其包含到 WordPress 设置中,您需要添加一个自定义.js文件并将其排队,直到 jQuery 作为依赖项加载到 WordPress 中。

粘性菜单.js

jQuery(document).ready(function($) {
    $(window).scroll(function(){
        //The Above Footer Widget Code
    });
});

函数.php

function stickyfooter_method() {
    wp_enqueue_script(
        'stickymenu',
         get_stylesheet_directory_uri() . '/js/stickymenu.js',
         array( 'jquery' )
    );
}

add_action( 'wp_enqueue_scripts', 'stickyfooter_method' );
于 2013-11-12T10:53:53.727 回答