简体   繁体   English

我可以使视差幻灯片在页面的中间而不是顶部停下来吗?

[英]Can i make a parallax slide stop halfway up the page instead of the top?

I have created a basic Parallax website here: 我在这里创建了一个基本的视差网站:

www.hrmny.co.uk www.hrmny.co.uk

The background images are fine, just how i want them. 背景图像很好,正是我想要的。

I want to change the white slides, when sliding from bottom to top, is there a way to automatically stops halfway up the page instead of stopping at the top? 我想更改白色幻灯片,当从底部滑动到顶部时,是否有一种方法可以自动在页面的中途停止而不是在顶部停止? I can post snippets of code to help, just tell me what you would like to see. 我可以发布一些代码片段来帮助您,只是告诉我您想看什么。 I'm new to this. 我是新来的。

Thanks for the help 谢谢您的帮助

a cheaty way : 欺骗的方式:

$("a[href=#slide4]").click()

It is not really the half way, it happens to be the middle until you change the page's content :) 这实际上不是一半,它恰好是中间,直到您更改页面内容为止:)

But how do you trigger the slide back up? 但是,如何触发幻灯片备份呢? Clicking on "home" link ? 点击“首页”链接? And then, you want to stop halfway instead of getting to the top ? 然后,您想中途停止而不是登顶?

so.. if the user is at the bottom of the page find a way to know that you want to change the "home" link to go to the "services" section? 等等..如果用户位于页面底部,则找到一种方法来知道您要更改“主页”链接以转到“服务”部分?

In addition to the target position you need to get the height of the viewport and the element you want to scroll to: 除了目标位置,您还需要获取视口的高度和要滚动到的元素:

var targetOffset = $target.offset().top;
var targetHeight = $target.outerHeight();
var viewportHeight = window.innerHeight;

Then adjust the target position by subtracting half the viewport height and adding half the element height: 然后通过减去一半视口高度并增加一半元素高度来调整目标位置:

var scrollTo = targetOffset + Math.round(targetHeight / 2) - Math.round(viewportHeight / 2);

I also moved the code for getting the position and heights into the click function so that it is calculated each time the user clicks, in case the user has resized the browser window or the element has changed position or height. 我还将用于获取位置和高度的代码移到click函数中,以便在用户调整浏览器窗口大小或元素更改位置或高度时,每次用户单击时都会计算该代码。

This is the final code from your site with the changes: 这是您网站中包含更改的最终代码:

// JQUERY FOR SLIDING NAVIGATION   
$(document).ready(function() {
  $('a[href*=#]').each(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
    && location.hostname == this.hostname
    && this.hash.replace(/#/,'') ) {
      var $targetId = $(this.hash), $targetAnchor = $('[name=' + this.hash.slice(1) +']');
      var $target = $targetId.length ? $targetId : $targetAnchor.length ? $targetAnchor : false;
      if ($target) {
        // JQUERY CLICK FUNCTION REMOVE AND ADD CLASS "ACTIVE" + SCROLL TO THE #DIV   
        $(this).click(function() {
          var targetOffset = $target.offset().top;
          var targetHeight = $target.outerHeight();
          var viewportHeight = window.innerHeight;
          var scrollTo = targetOffset + Math.round(targetHeight / 2) - Math.round(viewportHeight / 2);
          $("#nav li a").removeClass("active");
          $(this).addClass('active');
          $('html, body').animate({scrollTop: scrollTo}, 1000);
          return false;
        });
      }
    }
  });
});

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

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