简体   繁体   English

使用Parallax.js jQuery插件放大背景图片

[英]Background image zoomed in while using Parallax.js jQuery plugin

I added the parallax.js jQuery plugin and the effect works, but not properly. 我添加了parallax.js jQuery插件,并且效果正常,但是不正确。 The background is zoomed in and the image is glitchy on scroll. 背景放大,并且图像在滚动时出现故障。 I added additional variables listed in the documentation to fix those issues, and they work as expected. 我添加了文档中列出的其他变量来解决这些问题,并且它们按预期工作。 However, unexpectedly they also break my content which is created dynamically using JS in the '#sections' div, an empty div populated with a JavaScript object? 但是,出乎意料的是,它们也会破坏我在“ #sections” div(使用JavaScript对象填充的空div)中使用JS动态创建的内容?

function parallaxBackground (){
    var yPos = -(($window.scrollTop() - $this.offset().top) / 5);// Scroll speed      
    var coords = '1% '+ yPos + 'px';// Background position       
    $this.css({ backgroundPosition: coords });// Move the background
}   
parallaxBackground();//Call parallaxBackground function

Any idea why this function would erase HTML content created using JS? 知道为什么该函数会删除使用JS创建的HTML内容吗? The rest of the JS still works, its just the '#sections' div that stops working. JS的其余部分仍然有效,只是``#sections''div停止工作。 Scroll, reveal, and hover effects are still intact. 滚动,显示和悬停效果仍保持不变。 Here is a link to the code. 这是代码的链接。

https://gist.github.com/flyspaceage/075dcf3a6d6bd65edf0f456036eb9bd8 https://gist.github.com/flyspaceage/075dcf3a6d6bd65edf0f456036eb9bd8

Please read comment by FlySpaceAge for more info. 请阅读FlySpaceAge的评论以获取更多信息。

I ended up changing the script a bit and adding some variables from the parallax documentation . 我最终对脚本进行了一些更改,并从视差文档中添加了一些变量。 While this solution did fix the functionality, the background image is now zoomed in. While not perfect, this function did fix the initial problem I posted. 虽然此解决方案确实修复了该功能,但是现在放大了背景图像。虽然不完美,但此功能确实修复了我发布的最初问题。

(function($){
$(document).ready(function(){
    $('#sections').on('click', '.back-to-top', scrollToTop);
    $('#jump-menu li').click(scrollToAnchor);
    $('.parallax-window').parallax({imageSrc: '../images/background.png'});     
});

function scrollToTop(event){
    event.preventDefault();

    $('html, body').animate({
        scrollTop: 0
    }, 500);
}

function scrollToAnchor(event){
    event.preventDefault();

    var target = $(this).attr('rel');
    var offset = $(target).offset();

    $('html, body').animate({
        scrollTop: offset.top - 270
    }, 500);
}

/* PARALLAX functionality*/
function parallaxBackground (){

    var parallaxWidth = Math.max($(window).width() * 1, 1 ) | 0;

    $('.parallax-window').width(parallaxWidth); 

}

parallaxBackground();


})(jQuery);

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

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