Chrome浏览器滚动背景图片

时间:2018-07-26 07:18:52

标签: css google-chrome

以下代码在Firefox61和Chrome46之间的行为有所不同。框中的图像的行为与Firefox中预期的一样,在Firefox中,该图像被调整大小并占据了包含该图像的框的整个空间。

在Chrome中,仅将宽度(而不是高度)调整为框大小。因此,在滚动时,图像会在框内滚动。将max-height: 100%;添加到类bgimg不会改变行为。

如何在Chrome中获得预期的行为?知道为什么这种行为不同吗?

var Delighters = new (function() {
	var self = this,
			dels = this.dels = [],

			// default options
			options = {
				attribute: 	'data-delighter',
				classNames: ['delighter', 'started', 'ended'],
				start: 			0.75, // default start threshold
				end: 				0.75, // default end threshold
				autoInit: 	true 	// initialize when DOMContentLoaded
			};

	document.addEventListener("DOMContentLoaded", function() {
		if (options.autoInit) init();
	});

	function config(opts) {
		for (var name in opts) options[name] = opts[name];
	}
	
	function init() {		
		document.addEventListener('scroll', scroll)
		var els = document.querySelectorAll('[' + options.attribute + ']');

		for (var i=0; i<els.length; i++) {
			var el 			= els[i],
					def 		= el.getAttribute(options.attribute, 2),
					pairs 	= def.split(';'),
					del 		= {};
					del.start = options.start;
					del.end = options.end;
			
			for (var j=0; j<pairs.length; j++) {
				var pair 	= pairs[j].split(':'),
						name 	= pair[0],
						val 	= isNaN(pair[1] * 1)? pair[1] : pair[1] * 1;
				if (name) del[name] = (val === undefined)? true : val;
			}

			del.el = el;
			del.id = dels.length;
			dels.push(del);
			el.classList.add(options.classNames[0])
			if (del.debug) el.style.outline = 'solid red 4px';
		}
		scroll();
	}

	function scroll() {
		var viewportHeight = window.innerHeight;
		for (var i=0; i<dels.length; i++) {
			var del = dels[i],
					box = del.el.getBoundingClientRect(),
					factorStart = box.top / viewportHeight,
					factorEnd = box.bottom / viewportHeight;

			if (del.debug) {
				if (factorStart >= 0 && factorStart <= 1) {
					if (!del.startLine) {
						del.startLine = document.createElement('div')
						document.body.appendChild(del.startLine);
						del.startLine.style = 'position:fixed;height:0;width:100%;border-bottom:dotted red 2px;top:' + (del.start * 100) + 'vh';
					}
				}
				if (((factorEnd < del.end) || (factorStart > 1)) && del.startLine) {
					del.startLine.parentNode.removeChild(del.startLine);
					delete del.startLine;
				}
			}
			if (factorStart < del.start && !del.started) {
				del.started = true;
				del.el.classList.add(options.classNames[1])
			}
			else if (factorStart > del.start && del.started) {
				del.started = false;
				del.el.classList.remove(options.classNames[1])
			}
			if (factorEnd < del.end && !del.ended) {
				del.ended = true;
				del.el.classList.add(options.classNames[2])
			}
			else if (factorEnd > del.end && del.ended) {
				del.ended = false;
				del.el.classList.remove(options.classNames[2])
			}
		}
	}

	self.init = init;
	self.config = config;
})();
html, body {
  margin: 0;
}
article {
  font-size: 2em;
  font-family: futura, sans-serif;
  overflow-x: hidden;
}
@media (max-width: 460px) {
  article {
    font-size: 1.5em;
  }
}
section {
  position: relative;
  padding: 10vh 10vw;
  min-height: 100vh;
  color: #333;
}
section:nth-child(odd) {
  background: #4dc6e6;
  color: #fff;
}
section:nth-child(3) { background: #0aba58; color: #fff; }
section:nth-child(4) { background: #f8c82d; color: #333; }

.bgimg {
    max-width: 100%;
}

pre {
  font-size: .75em;
  background: #222; color: #fff;
  padding: 20px;
}
.box {
  position: relative;
  width: 50%; height: 30vh;
  background: #ec484d; margin: 10px; box-shadow: 0 0 10px rgba(0,0,0,.3);
  text-align: center;
  color: #fff;
  font-size: 0.8em;
  padding: 10px;
}
.box:nth-child(2) { background: #f8c82d; }
.box:nth-child(3) { background: #0aba58; }

.box4 {
  position: relative;
  width: 50%; height: 30vh;
  background: #ec484d; margin: 10px; box-shadow: 0 0 10px rgba(0,0,0,.3);
  text-align: center;
  color: #fff;
  font-size: 0.8em;
  padding: 10px;
}
.box:nth-child(2) { background: #f8c82d; }
.box:nth-child(3) { background: #0aba58; }
.box:nth-child(4) { background: #ffffff; }

.splashbox2 {
  position: relative;
  width: 50%; min-height: 100vh;
  background: #ec484d; margin: 10px; box-shadow: 0 0 10px rgba(0,0,0,.3);
  text-align: center;
  color: #fff;
  font-size: 0.8em;
  padding: 10px;
}
.delighter .splashbox2 { transition: all 1s ease-out; }
.delighter .splashbox2:nth-child(1) { transform: translate(-100%, 0); }
.delighter .splashbox2:nth-child(2) { transform: translate(200%, -100%); }

.delighter.started .splashbox2:nth-child(1) { transform: translate(0, 0); }
.delighter.started .splashbox2:nth-child(2) { transform: translate(100%, -100%); }

a {
  color: inherit;
}

.delighter.splash {
  transition: all 2s ease-out;
}
.delighter.splash.ended {
  background: #fff;
}
.delighter.right { transform:translate(-100%); opacity:0; transition: all .75s ease-out; }
.delighter.right.started { transform:none; opacity:1; }

.delighter.left { transform:translate(100%); opacity:0; transition: all .75s ease-out; }
.delighter.left.started { transform:none; opacity:1; }

.delighter.bottom { transform:translatey(300%); opacity:0; transition: all .75s ease-out; }
.delighter.bottom.started { transform:none; opacity:1; }

.delighter li { opacity: 0; transform: translatey(400%); transition: all .7s ease-out; }
.delighter.started li { opacity: 1; transform: none; }
.delighter.started li:nth-child(1) { transition: all .7s ease-out .1s; }
.delighter.started li:nth-child(2) { transition: all .7s ease-out .3s; }
.delighter.started li:nth-child(3) { transition: all .7s ease-out .5s; }
.delighter.started li:nth-child(4) { transition: all .7s ease-out .7s; }
.delighter.started li:nth-child(5) { transition: all .7s ease-out .9s; }
.delighter.started li:nth-child(6) { transition: all .7s ease-out 1.1s; }
.delighter.started li:nth-child(7) { transition: all .7s ease-out 1.3s; }

.delighter pre {
  display: block; transition: all 2s ease-out; opacity: 0;
  padding: 20px 0;
  width: 1px; overflow: hidden;
}
.delighter.started pre {
  max-width: 99999px; width: 100%; opacity: 1;
}
.delighter .box { transition: all 1s ease-out; }
.delighter .box:nth-child(1) { transform: translate(-100%, 0); }
.delighter .box:nth-child(2) { transform: translate(170%, -70%); }
.delighter .box:nth-child(3) { transform: translate(20%, 0%); }

.delighter.started .box:nth-child(1) { transform: translate(0, 0); }
.delighter.started .box:nth-child(2) { transform: translate(70%, -70%); }
.delighter.started .box:nth-child(3) { transform: translate(20%, -120%); }

.delighter .box4 { transition: all 1s ease-out; }
.delighter .box4:nth-child(1) { transform: translate(-100%, -70%); }
.delighter .box4:nth-child(2) { transform: translate(170%, -70%); }
.delighter .box4:nth-child(3) { transform: translate(=170%, =70%); }
.delighter .box4:nth-child(4) { transform: translate(200%, -90%); }

.delighter.started .box4:nth-child(1) { transform: translate(0, 0); }
.delighter.started .box4:nth-child(2) { transform: translate(100%, -105%); }
.delighter.started .box4:nth-child(3) { transform: translate(0%, -110%); }
.delighter.started .box4:nth-child(4) { transform: translate(100%, -215%); }

.shadowme { text-shadow: 3px 2px 5px rgba(0,0,0,0.87) };
<!doctype html>
<html>
  <head>
  </head>
  <body>
    <article>
  <section data-delighter class="splash">
     <h1 data-delighter class="right">Sport Services</h1>
     <h2 data-delighter class="left">Your full service partners.</h2>
     <p data-delighter="start:1" class="bottom">
        A series of corporate services. Register now:
     </p>
  </section>
      <section data-delighter="start:0.3">
         <div class="box4 bgimg shadowme" style='background:  url("https://s3-us-west-2.amazonaws.com/general-purpose-images/haley-phelps-433522-unsplash.jpg") no-repeat fixed; background-size: 100% 100%;'>Biz</div>
         <div class="box4 bgimg shadowme" style='background:  url("https://s3-us-west-2.amazonaws.com/general-purpose-images/joshua-earle-525853-unsplash.jpg") no-repeat fixed; background-size: 100% 100%;'>Tour</div>
         <div class="box4 bgimg shadowme" style='background:  url("https://s3-us-west-2.amazonaws.com/general-purpose-images/danielle-barnes-419252-unsplash2.jpg") no-repeat fixed; background-size: 100% 100%;' >Events </div>
         <div class="box4 bgimg shadowme" style='background:  url("https://s3-us-west-2.amazonaws.com/general-purpose-images/zoltan-tasi-679857-unsplash2.jpg") no-repeat fixed; background-size: 100% 100%;'>whatever </div>
      </section>
      <section>
         <h2 data-delighter class="right">Features</h2>
         <ul data-delighter>
            <li>Wide range of hospitality solutions</li>
            <li>Deep innventory of tickets to prestigious events</li>
            <li>Immediately confirmable services</li>
            <li>Autonomous systems</li>
            <li>API accessible</li>
         </ul>
      </section>
    </article>
  </body>
</html>

0 个答案:

没有答案