2015-03-24 13 views
18

यहाँ है सब कुछ की तुलना में धीमी बनाओ शरीर के लिए मेरे सीएसएस कोड है:(सीएसएस) एक पृष्ठभूमि छवि स्क्रॉल बाकी

body { 
    padding: 0; 
    margin: 0; 
    background-image: url("../images/background.jpg"); 
    background-repeat: no-repeat; 
    background-color: grey; 
    background-size: 100%; 
} 

मुझे क्या करना चाहते हैं यह इतना है कि बनाने के है छवि स्क्रॉल सब कुछ की तुलना में धीमी किसी और पर एक सरल लंबन प्रभाव बनाने के लिए पृष्ठ। मैंने ऑनलाइन देखा है और मैंने जो भी उदाहरण देखा है, उससे ज्यादा जटिल हैं जो मैं चाहता हूं।

+1

संभव नहीं है सीएसएस के साथ। –

+0

इससे https://github.com/pederan/Parallax-ImageScroll की सहायता मिलेगी, लेकिन सीएसएस के साथ यह संभव नहीं है कि सभी ने – ariestikto

उत्तर

35

मैंने अपनी लंबन गति में अधिक लचीलापन की तलाश में ठोकर खाई है जिसे मैंने शुद्ध सीएसएस के साथ बनाया है और मैं बस यह इंगित करना चाहता हूं कि सभी ये लोग गलत हैं और शुद्ध सीएसएस के साथ यह संभव है अपने तत्व की ऊंचाई को बेहतर ढंग से नियंत्रित करें।

आपको शायद कुछ कंटेनर तत्व रखने के लिए अपने डीओएम/एचटीएमएल को संपादित करना होगा, आपके मामले में आप शरीर को पृष्ठभूमि लागू कर रहे हैं जो आपको बहुत सीमित करेगा और एक अच्छा विचार नहीं लग रहा है।

https://stanhub.com/how-to-make-div-element-100-height-of-browser-window-using-css-only/

.forefront-element { 
    -webkit-transform: translateZ(999px) scale(.7); 
    transform: translateZ(999px) scale(.7); 
    z-index: 1; 
    } 

    .base-element { 
    -webkit-transform: translateZ(0); 
    transform: translateZ(0); 
    z-index: 4; 
    } 

    .background-element { 
    -webkit-transform: translateZ(-999px) scale(2); 
    transform: translateZ(-999px) scale(2); 
    z-index: 3; 
    } 

लेयर गति परिप्रेक्ष्य और का एक संयोजन के द्वारा नियंत्रित किया जाता है:

http://keithclark.co.uk/articles/pure-css-parallax-websites/

यहाँ कैसे आप स्क्रीन आकार के आधार व्यूपोर्ट-प्रतिशत lenghts साथ ऊंचाई पर नियंत्रण है जेड अनुवाद मूल्य। ऋणात्मक जेड मान वाले तत्व सकारात्मक मूल्य वाले लोगों की तुलना में धीमी गति से स्क्रॉल करेंगे। आगे का मान 0 से अधिक लंबित प्रभाव है (यानी translateZ (-10px) translateZ (-1px) से धीमी गति से स्क्रॉल करेगा)।

यहाँ, एक डेमो मैं एक गूगल खोज के साथ पाया क्योंकि मैं जानता हूँ कि वहाँ वहाँ गैर विश्वासियों के एक बहुत हैं है कभी नहीं कहते हैं कि असंभव:

http://keithclark.co.uk/articles/pure-css-parallax-websites/demo3/

+0

मैं यह स्पष्ट करना चाहता था कि यदि आप ब्राउज़र संगतता के बारे में चिंतित हैं तो यह अभी भी एक अच्छा समाधान नहीं है। इस लेखन के समय के नवीनतम फ़ायरफ़ॉक्स/क्रोम पर, एक श्वेत बार है जो प्रत्येक कंटेनर के बाईं ओर दिखाई देता है। इसके अलावा, क्रोम पर, पृष्ठभूमि तत्व बहुत अस्पष्ट दिखाई देते हैं। जबकि प्रभाव होता है, दृश्य उपस्थिति में अभी भी जेएस समाधान की तुलना में कमी है, और इसलिए यह शुद्ध सीएसएस समाधान काम नहीं करता है। –

+0

यह भी मोबाइल उपकरणों के स्क्रॉल प्रभाव को बदल रहा है। i: ई आईपैड और आईफोन चिपचिपा महसूस करने वाले स्पर्श को छोड़ने के बाद स्क्रॉलिंग जारी नहीं रखेंगे। हालांकि Jquery लंबन तकनीक के विपरीत, यह सफारी सहित सभी ब्राउज़रों में काम करता है। –

-2

jQuery के साथ ऐसा करने का सबसे अच्छा तरीका है। इस बारे में साइटों की एक बहुत कुछ उदाहरण के लिए, कर रहे हैं:

+1

सही नहीं किया है, यह वर्षों से सीएसएस के साथ संभव है। देखें [यहां] (http://keithclark.co.uk/articles/pure-css-parallax-websites/) – ivarni

+0

मैंने अपना जवाब संपादित किया, – linuscl

2

सहमत कि यह सिर्फ सीएसएस के साथ संभव नहीं है, क्योंकि आप की गणना करने के लिए है छवि और दस्तावेज़ ऊंचाई अनुपात। मुझे यह प्रभाव भी पसंद है, यही कारण है कि सरल फ़ंक्शन बनाया गया है जो कि बस करता है। यहाँ समारोह और स्क्रॉल घटना पर अपनी कॉल है:

$(window).on('scroll', function() { 
 
\t smoothBackgroundScroll("relative/image/url"); 
 
}); 
 

 
function smoothBackgroundScroll(imgsrc) { 
 
\t function loadImageHeight(url, width) { 
 
\t \t var img = new Image(); 
 
\t \t img.src = url; 
 
\t \t if (width) { 
 
\t \t \t img.width = width; 
 
\t \t } 
 
\t \t return img.height; 
 
\t } 
 

 
\t var dh, wh, ih, st, posy, backh, backw; 
 
\t if (!this._smoothBackgroundScroll) { 
 
\t \t var bcksize = $(document.body).css('background-size'); 
 
\t \t var bmatch = /(\w+)\s*(\w+)/.exec(bcksize); 
 
\t \t if (!bmatch || bmatch.length < 3) { 
 
\t \t \t backh = loadImageHeight(imgsrc) 
 
\t \t } else { 
 
\t \t \t backh = parseInt(bmatch[2]); 
 
\t \t \t if (isNaN(backh)) { 
 
\t \t \t \t backw = parseInt(bmatch[1]); 
 
\t \t \t \t backh = loadImageHeight(imgsrc, parseInt(backw)); 
 
\t \t \t } 
 
\t \t } 
 
\t \t this._smoothBackgroundScroll = { 
 
\t \t \t dh: $(document).height() 
 
\t \t \t , wh: $(window).height() 
 
\t \t \t , ih: backh 
 
\t \t } 
 
\t } 
 
\t dh = this._smoothBackgroundScroll.dh; 
 
\t wh = this._smoothBackgroundScroll.wh 
 
\t ih = this._smoothBackgroundScroll.ih; 
 
\t st = $(document).scrollTop(); 
 
\t posy = (dh - ih) * st/(dh - wh); 
 
\t document.body.style.backgroundPosition = 'center ' + posy + 'px'; 
 
}

आप इसे यहाँ पा सकते हैं उदाहरण और दृश्य व्याख्या क्या वास्तव में छवि और दस्तावेज़ के साथ हो रहा है के साथ:

Smooth background image scrolling

+1

जानना अच्छा नहीं है, यह सीएसएस के साथ वर्षों से संभव है। देखें [यहां] (http://keithclark.co.uk/articles/pure-css-parallax-websites/) – ivarni

2

यदि आप एक उच्च पृष्ठभूमि छवि को लागू करना चाहते हैं, तो इस जेएस का उपयोग करें:

(function() { 
     var body = document.body, 
       e = document.documentElement, 
       scrollPercent; 
     $(window).unbind("scroll").scroll(function() { 
      scrollPercent = 100 * $(window).scrollTop()/($(document).height() - $(window).height()); 
      body.style.backgroundPosition = "0px " + scrollPercent + "%"; 
     }); 
})(); 
+0

यह काफी सरल है और अच्छी तरह से काम करता है। सीएसएस समाधानों को भी अनचाहे करना यह धुंधला और भयानक लग रहा है। +1 – Eolis

1

मुझे लगता है कि यह एक पुराने सवाल यह है कि, हालांकि, मैंने हाल ही में इस समस्या पर खुद को ठोकर खाई और सर्वोत्तम कामकाजी कोड खोजने की कोशिश में काफी समय बिताया। जो कुछ भी मैंने पाया वह बहुत जटिल था या विशेष रूप से क्रोम में बहुत कम किए बिना काम नहीं किया। के रूप में दूसरों के द्वारा ने कहा, समस्या शुद्ध सीएसएस द्वारा हल नहीं किया जा सकता है, लेकिन मैं अपने सरल AngularJS समस्या को हल करने के निर्देश दिया:

app.directive("paraBack", ['$window', function ($window) { 
    return function(scope, element, attrs) { 
    element.css("background-image", "url("+attrs.paraBack+")"); // Apply the background image with CSS 
    element.css("background-attachment", "fixed"); // Disable background scrolling 

    var max = Infinity; 

    var image = new Image(); // Create a JavaScript image so that the code below can be run when the background is loaded 
    image.src = attrs.paraBack; 
    image.onload = function() { 
     max = image.height - window.innerHeight; // Stop scrolling after the bottom of the picture is reached 
     var xOffset = -(image.width/2-window.innerWidth/2); 
     element.css("background-position-x", xOffset+'px'); // Horizontally align the background 
    } 

    var scrollHandler = function() { 
     var offset = Math.floor(this.pageYOffset*0.1); // Set background to scroll at 10% of scrolling speed 
     if (offset<max) { 
     element.css('background-position-y', '-'+offset+'px'); // If not the bottom of the image is reached, move the background (scroll) 
     } 
    }; 

    angular.element($window).on('scroll', scrollHandler); // Set the defined scrollHandler function to be ran when user scroll 

    scope.$on('$destroy', function() { 
     angular.element($window).off('scroll', scrollHandler); // Unbind the function when the scope is destroyed 
    }); 
    }; 
}]); 

यह इस तरह html में इस्तेमाल किया जा सकता:

<body para-back="url/to/image"> 

यदि आप ऐसा दिखने का उदाहरण देखना चाहते हैं, तो आप this page पर जा सकते हैं।

0

आप कुछ यहाँ की तरह साधारण उपयोग कर सकते हैं: एचटीएमएल:

मोशन

सीएसएस:

body { 

    min-height:4000px; 
    background-image: url("http://www.socialgalleryplugin.com/wp-content/uploads/2012/12/social-gallery-example-source-unknown-025.jpg"); 
} 

h1 {margin-top:300px;} 

js:

(function(){ 

    var parallax = document.querySelectorAll("body"), 
     speed = 0.5; 

    window.onscroll = function(){ 
    [].slice.call(parallax).forEach(function(el,i){ 

     var windowYOffset = window.pageYOffset, 
      elBackgrounPos = "50% " + (windowYOffset * speed) + "px"; 

     el.style.backgroundPosition = elBackgrounPos; 

    }); 
    }; 

})(); 

यहाँ jsfiddle

संबंधित मुद्दे