在滚动点上创建侧边栏淡化

时间:2015-07-18 18:46:53

标签: javascript jquery html css

正在研究某种方法让我的侧导航栏在我网站的特定区域上淡出。只是不知道如何去做。我找到了一些围绕问题的jquery。但是对这些东西不熟悉我不知道如何将它实现到我的特定代码中。

给出的例子是

if ($(window).scrollTop() >= "number of pixels") {
    if ($('"button plus number"').css('display') === 'none') {
        $('"button plus number"').fadeIn('slow');
        $('"button plus number"').prev().fadeOut();
        $('"button plus number"').next().fadeOut();
    }
}

所以基本上我的代码我想要的是让.cbp-fbscroller淡入或至少出现在900px左右。一旦我理解了它是如何工作的,我就可以使用代码使其他东西在滚动点上淡入。

这是一个基本的小提琴,所以你们可以得到这个想法http://jsfiddle.net/vLf18Lbk/

fadein的HTML区域:

<div class="main">   
    <div id="cbp-fbscroller" class="cbp-fbscroller">
        <nav>
            <a href="#fbsection1" class="cbp-fbcurrent">Section 1</a>
            <a href="#fbsection2">Section 2</a>
            <a href="#fbsection3">Section 3</a>
            <a href="#fbsection4">Section 4</a>
            <a href="#fbsection5">Section 5</a>
        </nav>
        <section id="fbsection1"></section>
        <section id="fbsection2"></section>
        <section id="fbsection3"></section>
        <section id="fbsection4"></section>
        <section id="fbsection5"></section>
    </div>
</div>    

需要淡入的CSS:

/* The nav is fixed on the right side  and we center it by translating it 50% 
(we don't know it's height so we can't use the negative margin trick) */
.cbp-fbscroller > nav {
    position: fixed;
    z-index: 9999;
    right: 100px;
    top: 50%;
    width: 26px;
    -webkit-transform: translateY(-50%);
    -moz-transform: translateY(-50%);
    -ms-transform: translateY(-50%);
    transform: translateY(-50%);
}

1 个答案:

答案 0 :(得分:0)

你的jQuery代码是正确的。 当你滚动到底部超过250px淡入&#34;去顶部&#34;,在其他方面淡出&#34;去顶部&#34; 你可以查看javascript的第41行

&#13;
&#13;
$(document).ready(function () {
    $(document).on("scroll", onScroll);
    
    //smoothscroll
    $('a[href^="#"]').on('click', function (e) {
        e.preventDefault();
        $(document).off("scroll");
        
        $('a').each(function () {
            $(this).removeClass('active');
        })
        $(this).addClass('active');
      
        var target = this.hash,
            menu = target;
        $target = $(target);
        $('html, body').stop().animate({
            'scrollTop': $target.offset().top+2
        }, 500, 'swing', function () {
            window.location.hash = target;
            $(document).on("scroll", onScroll);
        });
    });
});

function onScroll(event){
    var scrollPos = $(document).scrollTop();
    $('#menu-center a').each(function () {
        var currLink = $(this);
        var refElement = $(currLink.attr("href"));
        if (refElement.position().top <= scrollPos && refElement.position().top + refElement.height() > scrollPos) {
            $('#menu-center ul li a').removeClass("active");
            currLink.addClass("active");
        }
        else{
            currLink.removeClass("active");
        }
    });
}

///// edit go to top
$(window).scroll(function() {
    if ($(this).scrollTop() > (250)) {
        $("#top").fadeIn('fast');
    } else{
        $("#top").fadeOut('fast');
    };
});


$("a[href='gototop']").click(function() {
  $("html, body").animate({ scrollTop: 0 }, "slow");
  return false;
});
&#13;
body, html {
    margin: 0;
    padding: 0;
    height: 100%;
    width: 100%;
}
.menu {
    width: 100%;
    height: 75px;
    background-color: rgba(0, 0, 0, 1);
    position: fixed;
    background-color:rgba(4, 180, 49, 0.6);
    -webkit-transition: all 0.3s ease;
    -moz-transition: all 0.3s ease;
    -o-transition: all 0.3s ease;
    transition: all 0.3s ease;
}
.light-menu {
    width: 100%;
    height: 75px;
    background-color: rgba(255, 255, 255, 1);
    position: fixed;
    background-color:rgba(4, 180, 49, 0.6);
    -webkit-transition: all 0.3s ease;
    -moz-transition: all 0.3s ease;
    -o-transition: all 0.3s ease;
    transition: all 0.3s ease;
}
#menu-center {
    width: 980px;
    height: 75px;
    margin: 0 auto;
}
#menu-center ul {
    margin: 15px 0 0 0;
}
#menu-center ul li {
    list-style: none;
    margin: 0 30px 0 0;
    display: inline;
}
.active {
    font-family:'Droid Sans', serif;
    font-size: 14px;
    color: #fff;
    text-decoration: none;
    line-height: 50px;
}
a {
    font-family:'Droid Sans', serif;
    font-size: 14px;
    color: black;
    text-decoration: none;
    line-height: 50px;
}
#home {
    background-color: grey;
    height: 100%;
    width: 100%;
    overflow: hidden;
    background-image: url(images/home-bg2.png);
}
#portfolio {
    background-image: url(images/portfolio-bg.png);
    height: 100%;
    width: 100%;
}
#about {
    background-color: blue;
    height: 100%;
    width: 100%;
}
#contact {
    background-color: red;
    height: 100%;
    width: 100%;
}
#top{
    position: fixed;
    bottom: 5px;
    right: 5px;
    background-color: #ffff00;
    cursor: pointer;
    display: none;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="m1 menu">
    <div id="menu-center">
        <ul>
            <li><a class="active" href="#home">Home</a>

            </li>
            <li><a href="#portfolio">Portfolio</a>

            </li>
            <li><a href="#about">About</a>

            </li>
            <li><a href="#contact">Contact</a>

            </li>
        </ul>
    </div>
</div>
<div id="home"></div>
<div id="portfolio"></div>
<div id="about"></div>
<div id="contact"></div>
<a href="gototop" id="top">go to top</a>
&#13;
&#13;
&#13;

以同样的方式,您可以选择每个标签淡入淡出或外观。