css制作固定背景,全屏滑动固定背景

所属分类:背景类

 8578次|    69次   点击收藏(5)

css制作固定背景,全屏滑动固定背景

一、说明

这个案例是用纯css3制作的,滑动时,文字会滚动移动走,而背景是不变的。效果大家可以点击demo预览一下!


二、原理

相对定位的方式,一层滑动背景,一层固定背景。

滑动背景和固定背景都设置:


    position: relative;
    min-height: 100%;

当滑动背景滚动走了,出现固定背景。固定背景设置:
    background-attachment: fixed;

三:部分代码

css代码:
.cd-main-content {
    /* you need to assign a min-height to the main content so that the children can inherit it*/
    height: 100%;
    position: relative;
    z-index: 1;
}

.cd-fixed-bg {
    position: relative;
    min-height: 100%;
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center center;
    z-index: 1;
}
.cd-fixed-bg h1, .cd-fixed-bg h2 {
    position: absolute;
    left: 50%;
    top: 50%;
    bottom: auto;
    right: auto;
    -webkit-transform: translateX(-50%) translateY(-50%);
    -moz-transform: translateX(-50%) translateY(-50%);
    -ms-transform: translateX(-50%) translateY(-50%);
    -o-transform: translateX(-50%) translateY(-50%);
    transform: translateX(-50%) translateY(-50%);
    width: 90%;
    max-width: 1170px;
    text-align: center;
    font-size: 30px;
    font-size: 1.875rem;
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
    color: white;
}
.cd-fixed-bg.cd-bg-1 {
    background-image: url("../images/cd-background-1.jpg");
}
.cd-fixed-bg.cd-bg-2 {
    background-image: url("../images/cd-background-2.jpg");
}
.cd-fixed-bg.cd-bg-3 {
    background-image: url("../images/cd-background-3.jpg");
}
.cd-fixed-bg.cd-bg-4 {
    background-image: url("../images/cd-background-4.jpg");
}
@media only screen and (min-width: 768px) {
    .cd-fixed-bg h1, .cd-fixed-bg h2 {
        font-size: 36px;
    }
}
@media only screen and (min-width: 1170px) {
    .cd-fixed-bg {
        background-attachment: fixed;
    }
    .cd-fixed-bg h1, .cd-fixed-bg h2 {
        font-size: 48px;
        font-weight: 300;
    }
}

.cd-scrolling-bg {
    position: relative;
    min-height: 100%;
    padding: 4em 0;
    line-height: 1.6;
    box-shadow: 0 0 50px rgba(0, 0, 0, 0.5);
    z-index: 2;
}
.cd-scrolling-bg.cd-color-1 {
    background-color: #3d3536;
    color: #a6989a;
}
.cd-scrolling-bg.cd-color-2 {
    background-color: #99a478;
    color: #3d3536;
}
.cd-scrolling-bg.cd-color-3 {
    background-color: #b4d7a8;
    color: #3d3536;
}
@media only screen and (min-width: 768px) {
    .cd-scrolling-bg {
        padding: 8em 0;
        font-size: 20px;
        font-size: 1.25rem;
        line-height: 2;
        font-weight: 300;
    }
}





关于前端资源库

前端资源库是haorooms博主平时积累的前端资源,大部分来源于互联网。但经过博主调试和加工,集聚了博主的心血。

因此,资源库所有资源,仅供学习和参考,切勿下载下来用于商业用途或者转卖。转载请注明来自haorooms博客资源库!

联系方式
关键词搜索