简体   繁体   English

恒星视差可在桌面上运行,在移动设备上为静态图像

[英]Stellar Parallax to run on desktop, static image on mobile device

My website is using Stellar.js to create a parallax effect on a number of images that cover the width of the users screen. 我的网站使用Stellar.js在许多覆盖用户屏幕宽度的图像上创建视差效果。 Stellar scrolls across the image at half the speed the user scrolls down the page creating a nice effect. 恒星在图像上滚动的速度是用户向下滚动页面的一半,从而产生了不错的效果。 I originally used this code: 我最初使用此代码:

 MY CSS FILE /* Separator About - Parallax Section */ .sep { background-attachment: fixed!important; background-position: 50% 0!important; background-repeat: no-repeat!important; width: 100%!important; height: 180px!important; position: relative!important; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; } .about { background-image: url(../img/about-sep.jpg); 
 MY HTML FILE <! -- ABOUT SEPARATOR --> <div class="sep about" data-stellar-background-ratio="0.5"></div> </div> </div> <script src="assets/js/jquery.stellar.min.js"></script> <script> $(function(){ $.stellar({ horizontalScrolling: false, verticalOffset: 40 }); }); </script> </body> 

I discovered if I change background attachment from fixed to scrolled, the parallax effect would work on both desktop and ios. 我发现,如果将背景附件从固定更改为滚动,则视差效果将在台式机和ios上均起作用。 Although a little choppy on ios, and tricky to configure when user is switching between landscape and portrait. 虽然在ios上有些混乱,但是当用户在横向和纵向之间切换时很难进行配置。 For this reason - made stellar responsive, which seems to help. 由于这个原因-使恒星响应,这似乎有所帮助。 New code is: 新代码是:

 //JAVASCRIPT $(function(){ $.stellar({ horizontalScrolling: false, // Refreshes parallax content on window load and resize responsive: true, verticalOffset: 40 }); }); 
 //CSS .sep { background-attachment: scroll; background-position: 50% 0; background-repeat: no-repeat; height: 180px; position: relative; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; } .about { background-image: url(../img/about-sep.jpg); 
 //HTML <div class="sep about" data-stellar-background-ratio="0.5"></div> </div> </div> 

If I decide that it is too choppy/unpredictable on mobile - I could add this code to my javascript: 如果我认为它在移动设备上过于混乱/无法预测-我可以将此代码添加到我的JavaScript中:

 // Stellar Parallax Script var isMobile = { Android: function() { return navigator.userAgent.match(/Android/i); }, BlackBerry: function() { return navigator.userAgent.match(/BlackBerry/i); }, iOS: function() { return navigator.userAgent.match(/iPhone|iPad|iPod/i); }, Opera: function() { return navigator.userAgent.match(/Opera Mini/i); }, Windows: function() { return navigator.userAgent.match(/IEMobile/i); }, any: function() { return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows()); } }; if( !isMobile.any() ) $(function(){ $.stellar({ horizontalScrolling: false, // Refreshes parallax content on window load and resize responsive: true, verticalOffset: 40 }); }); 

This code successfully gives me a static image with same dimensions on mobile - and gives me the parallax scroll effect on desktops. 此代码成功地为我提供了在移动设备上具有相同尺寸的静态图像-并为我提供了在台式机上的视差滚动效果。

First of all, thanks a lot for sharing your code! 首先,非常感谢您分享您的代码! i had really bad times trying to fix this and you helped me. 我真的很难解决这个问题,而你帮助了我。 I just wanted to share what i've used in order to avoid lagging by using "scroll" instead of "fixed"...this worked for me, perfect parallax on desktop using stellar.js and fixed img on mobile and device. 我只想分享我使用的内容,以避免使用“滚动”而不是“固定”来拖延时间……这对我有用,可以使用stellar.js在移动设备和设备上固定img,在桌面上实现完美的视差。 Hope could be useful! 希望可能有用!

<script>
var isMobile = {
                Android: function() {
                    return navigator.userAgent.match(/Android/i);
                },
                BlackBerry: function() {
                    return navigator.userAgent.match(/BlackBerry/i);
                },
                iOS: function() {
                    return navigator.userAgent.match(/iPhone|iPad|iPod/i);
                },
                Opera: function() {
                    return navigator.userAgent.match(/Opera Mini/i);
                },
                Windows: function() {
                    return navigator.userAgent.match(/IEMobile/i);
                },
                any: function() {
                    return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
                }
            };

            $(document).ready(function() {
                if (isMobile) {
                    $(".section1Paral").css("background-attachment", "scroll");
                };
            });

            if( !isMobile.any() )
                $(function(){
                        $.stellar({
                            horizontalScrolling: false,
                            // Refreshes parallax content on window load and resize
                            responsive: true,
                            verticalOffset: 40
                        });
            });
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM