【问题标题】:CSS Parallax image issues with image cutting off图像切断的CSS视差图像问题
【发布时间】:2016-07-01 09:12:38
【问题描述】:

在这个网站 (http://rvwinvesting.com/2016/) 上遇到问题,我有两张视差图像。一张是中间的无缝背景,另一张是页面底部页脚附近一系列办公楼的照片。这就是问题所在。当您向上滚动时,页面的底部和底部会出现,因此看起来很奇怪,而不是一个滚动图像。有什么办法可以解决这个问题吗?图像的高度超过 1200 像素,而 div 只有 600 像素,我也尝试过更小的 div 尺寸,无论如何,都会出现接缝。这是该 div 的 CSS:

#building {
    background: url(http://rvwinvesting.com/2014/wp-content/uploads/2014/09/rvw-building.jpg) 50% 0 fixed;
    height: 600px;
    -webkit-background-size: cover!important;
    -moz-background-size: cover!important;
    -o-background-size: cover!important;
    background-size: cover!important;
 }

Tried100%100% for background-size 但这只会拉伸照片。不好。

【问题讨论】:

  • 你能把问题解释清楚一点吗?我不明白什么是错的或你想做什么。如果页脚和页面的其余部分看起来很奇怪,请降低 #building 的高度并在其前面添加一个半透明的黑色 div 或使用照片编辑器降低图片的亮度。
  • 当然。基本上,随着滚动的进行,建筑物的照片会向上移动,但您可以看到照片顶部和底部之间的接缝。所以基本上这张照片(我猜)从顶部的中间开始,一旦到达底部,就会走到中间。应该发生的是照片从顶部开始并移动到底部。你永远看不到接缝。

标签: html css image background-image parallax


【解决方案1】:

我不是 100% 确定我是否清楚地理解了您的问题,但如果您正在谈论在底部构建的重复问题,这是我的建议:

  1. 您不必在background-size 之后使用!important(只需稍微清除您的代码)
  2. 在背景上添加no-repeat 以避免多个建筑物背景。
  3. 仅在建筑背景位于视口上或视口之前开始视差。在您的情况下,当#building 到达屏幕底部时,启动视差效果。

希望它会有所帮助。


2016 年 3 月 22 日更新

我下载了这个主题(BootstrapParallax)并围绕它玩,请执行以下操作以仅在它出现时启动您的视差:

**将新的“数据”属性添加到您想要的div/sectiondata-start-at="bottom" 所以你的div/section 会是这样的:

<section id="building" data-speed="4" data-type="background" data-start-at="bottom">

之后,在你的主题文件夹中找到这个 .js 文件(路径:js/strap-extras.js)并替换为以下代码:

// Placeholder
  jQuery(document).ready(function(){
     // cache the window object
     jQuerywindow = jQuery(window);

     jQuery('section[data-type="background"]').each(function(){
       // declare the variable to affect the defined data-type
       var jQueryscroll = jQuery(this),
          startAt = jQueryscroll.data('start-at'),
          yPos, coords, thisOffset;

        jQuery(window).scroll(function() {
          // HTML5 proves useful for helping with creating JS functions!
          // also, negative value because we're scrolling upwards    

          if(!startAt) {

             yPos = -(jQuerywindow.scrollTop() / jQueryscroll.data('speed'));

            // background position
            coords = '50% '+ yPos + 'px';

          } else {
            thisOffset = jQueryscroll.offset();
            thisTop = thisOffset.top;

            //console.log(thisTop - jQuerywindow.outerHeight(), ' wondow: ', jQuerywindow.scrollTop());

            if( (thisTop - jQuerywindow.outerHeight()) < jQuerywindow.scrollTop() ) {

               yPos = -(jQuerywindow.scrollTop() / jQueryscroll.data('speed'));
              // background position
              coords = '50% '+ yPos + 'px';

            }

          }

          // move the background
          jQueryscroll.css({ backgroundPosition: coords });   
        }); // end window scroll
     });  // end section function
  }); // close out script

  document.createElement("section");

PS:js 可以写得更清楚更高效,这只是一个例子。

而且,请清除你的 css,你不需要到处都有!important

你干净的 CSS 可以是这样的:

#building {
    background: url(/wp-content/uploads/2014/09/rvw-building.jpg) no-repeat 50% 0 fixed;
    background-size: cover;
    position: relative;
    width: 100%;
    height: 600px;
}

您可能需要稍作调整,但希望它能解决您的问题。

否则,你必须学习如何编写js/css :)

享受吧!

-- 金巴

【讨论】:

  • 添加 no-repeat 只会给 div 带来很多白色。不好。
  • @AdamBell 看看我在#3 中的建议。只有当 div 到达屏幕底部时,您才必须为 #building div 启动视差效果。
  • 我不明白该怎么做。我尝试了 'top' 和 0px,但没有奏效。
  • @AdamBell 它仍然在移动设备上显示重复的图像。添加填充不适用于所有屏幕尺寸。如果您尝试了解视差的工作原理并遵循 Jimba Tamang 的建议,那就更好了。仅在需要时启动视差也会提高滚动性能。
【解决方案2】:

我想我终于让它工作了。 padding-top 设置似乎使一切变得不同。这是新代码:

#building {
    background: url(http://rvwinvesting.com/2014/wp-content/uploads/2014/09/rvw-building.jpg) 50% 0 fixed;
    background-position: top center !important;
    background-size: cover !important;
    background-repeat: no-repeat !important;
    margin: 0 auto !important;
    padding-top: 250px;
    position: relative;
    width: 100%;
    height: 600px;
 }

【讨论】:

  • 对我来说它是在做“背景位置:顶部中心;”而不是“背景位置:顶部;”。谢谢分享!
【解决方案3】:

图片的顶部和底部被裁剪掉,因为#building 比图片宽。您可以使 div 更长或找到更宽的图像。

【讨论】:

  • 但是你应该使用两倍于实际 div 高度的图像吗?因此,如果 div 的高度为 500px....图像不应该是 1000px 的高度吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-05
  • 1970-01-01
  • 2021-12-22
  • 1970-01-01
  • 2015-10-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多