简体   繁体   English

淡入,根据滚动淡出

[英]Fade in, fade out according to scrolling

I was recently given a script to change the positioning of a div to fixed after certain scrolling. 最近给了我一个脚本,该脚本可以在某些滚动后将div的位置更改为fixed。 I've plyaed with it a bit and learnt I can change other stuff besides position, like backgrounds. 我已经对此感到满意,并了解到我可以更改职位以外的其他内容,例如背景。 I've got a logo that changes according to the scrolling, but the change is a bit too abrupt and I'd like to make the change smoother with a fade in and fade out. 我有一个随滚动而变化的徽标,但是更改有点太突然了,我想通过淡入和淡出使更改更平滑。 The problem is, as I've mentioned in other questions I've made, I'm a noob at scripting, although I've been learning a little with people in here helping me to get solutions. 问题是,正如我在其他问题中提到的那样,尽管我在这里与人们一起学习了一些帮助我获得解决方案的方法,但是我对脚本编写还是一无所知。

To make clearer what I want, I want a logo that does the same as the logo of this website when scrolling: https://www.planetside2.com/news 为了更清楚我想要的内容,我想要一个与滚动时此网站的徽标相同的徽标: https : //www.planetside2.com/news

This is my HTML: 这是我的HTML:

<div id="wrap">
 <div id="page-header">
  <div class="headerbar">
   <li class="logo">
    <a>Logo is here</a>
   </li>
  </div>
 </div>
</div>

The script I was given with a few modifications of mine: 给我的脚本做了一些修改:

$(document).ready(function(){

    $(window).on("scroll resize", function(e){

        var elem = $(".headerbar");
        var shadow = $(".headerbar .menu");
        var logo = $(".logo a");

        if ((elem.offset().top - $(window).scrollTop()) <= -73 && elem.css("position") !== "fixed") {
            console.log("not visible");
            elem.css({
                position:"fixed",
                "z-index":"9999",
                top:"-71px"
            });
            shadow.css({
                "box-shadow":"0 15px 20px -3px #000"
            });
            logo.css({
                "background":" url('{T_THEME_PATH}/images/scrolledlogo.png') center no-repeat",
                "width":" 213px",
                left: "-34px",
                top:"-41px"
            });

        } else if (elem.height() >= $(window).scrollTop()) {
            console.log("visible");
            elem.css({
                position:"relative",
                top:"0px"
            });
            shadow.css({
                "box-shadow":"none"
            });
            logo.css({
                "background":" url('{T_THEME_PATH}/images/logo.png') center no-repeat",
                "width":"143px",
                left: "0px",
                top:"-40px"
            });
        }

    });

})

The website I want to apply all of this to: http://etrostruewowdesigncomplete.esy.es/phpBB3/viewtopic.php?f=2&t=1 我想将所有这些都应用到的网站: http : //etrostruewowdesigncomplete.esy.es/phpBB3/viewtopic.php?f=2&t=1

You can just fade one logo into another triggered at a cerain point 您可以将一个徽标逐渐淡入另一个徽标中

DEMO http://jsfiddle.net/hpXL4/138/ 演示http://jsfiddle.net/hpXL4/138/

$(window).scroll(function () {
    var y_scroll_pos = window.pageYOffset;
    var scroll_pos_test = 150;
    // set to whatever you want it to be

    if (y_scroll_pos > scroll_pos_test) {
        $(".apple").fadeIn();
        $(".apple2").fadeOut();
    } else {
        $(".apple2").fadeIn();
        $(".apple").fadeOut();
    }
});

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

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