繁体   English   中英

BG图像上的视差H1

[英]Parallax H1 Over BG Image

我在背景图片上方有一个h1,

<section class="cd-intro">
    <h1>Content</h1>
</section>`

使用以下css:

.cd-intro {
    position: relative;
    height: 100%;
    background: url("../img/buff.jpg") no-repeat center center;
    background-size: cover;
    z-index: 1;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

我希望尽可能简洁地在大致复制此效果 输入赞赏。

之前已经问过有关基本视差效果的问题因此如果您仍然对此感到疑惑,请查看一下stackoverflow。

至于视差头移动,最简单的答案是:

jQuery(window).scroll(function() {
    //Get the scoll position of the page
    scrollPos = jQuery(this).scrollTop();

    //Scroll and fade out the banner text
    jQuery('.featured-title').css({
      'margin-top' : -(scrollPos/3)+"px",
      'opacity' : 1-(scrollPos/300)
    });    
});

我想到的是通过查看网站上的源javascript。 您将在main.js看到:

///////////////////////////////
// Parallax Scrolling
///////////////////////////////

// Calcualte the home banner parallax scrolling
function scrollBanner() {
    //Get the scoll position of the page
    scrollPos = jQuery(this).scrollTop();

    //Scroll and fade out the banner text
    jQuery('.featured-title').css({
      'margin-top' : -(scrollPos/3)+"px",
      'opacity' : 1-(scrollPos/300)
    });
}


jQuery(document).ready(function(){
///////////////////////////////
// Initialize Parallax 
/////////////////////////////// 
    if(!isMobile()) {
        jQuery(window).scroll(function() {        
                scrollBanner();       
        });
    }
    else {
        jQuery('#banner-text').css({
            'position': 'relative',
            'text-align': 'center',
            'margin': 'auto',
            'padding': '0',
            'top':'0'
        });
    }
    /* bunch of other stuff */
}

这里发生的是正在监听window对象上的scroll事件。 触发此事件时, margin-top限值和opacity将与滚动距离成比例地更改(请参见scrollBanner函数)。

暂无
暂无

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

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