首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BG图像上的视差H1

问BG图像上的视差H1
EN

Stack Overflow用户
提问于 2015-06-06 20:38:57
回答 1查看 306关注 0票数 0

我在背景图像上有一个h1,

代码语言:javascript
复制
<section class="cd-intro">
    <h1>Content</h1>
</section>`

使用以下css:

代码语言:javascript
复制
.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;
}

我希望能或多或少准确地复制这里的效果,尽可能简洁。投入感谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-06 21:22:36

关于基本视差效应在此之前的问题已经被问到了,所以如果你还在想这个问题的话,看看堆叠溢出吧。

关于视差标头运动,简短的回答是:

代码语言:javascript
复制
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中看到

代码语言:javascript
复制
///////////////////////////////
// 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 */
}

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30687360

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档