简体   繁体   English

在页面上的Wordpress循环中从一个帖子滚动到另一个帖子,并显示所有帖子

[英]Scroll from post to post in Wordpress loop on page with all posts displaying

I have a Tumblr-like blog feature that displays an image followed by some text in a div. 我有一个类似于Tumblr的博客功能,该功能在div中显示图像,然后显示一些文本。 This is repeated for as many posts as the the custom post-type contains using a loop, like so: 对于使用循环的自定义帖子类型所包含的帖子,重复此操作的次数如下:

<?php

        // The Arguments
        $args = array(
            'post_type' => 'strange',
            'posts_per_page' =>-1, 
            'order_by' => 'menu_order'

        );

        $c = 0; // this starts the count at the beginning
        $class = ''; // standard is no class

        // The Query
        $the_query = new WP_Query( $args ); ?>

        <?php

        // If we have the posts...
        if ( $the_query->have_posts() ) : ?>

        <!-- Start the loop the loop --> 
            <?php while ( $the_query->have_posts() ) : $the_query->the_post(); 
            $c++; // this makes the loop count each post 
            if ( $c == 1 ) $class .= ' current'; // if the item is the first item, add class current
            else $class = ''; // if it's not the first item, don't add a class 
            ?>

                <div id="strange-container" class="strange-container <?php echo $class; ?>">    

                    <img src="<?php the_field('strange-img'); ?>" alt="<?php the_title(); ?>" />

                    <span><?php the_field('strange-text'); ?></span>

                </div>

            <?php endwhile; endif; // end of the loop. ?>

        <?php wp_reset_postdata(); ?>

I have a navigation arrow at the top of the page. 我在页面顶部有一个导航箭头。 On click, I want it to scroll the user to the div in the loop. 单击时,我希望它将用户滚动到循环中的div。 I have this working using this jQuery that adds a class/removes a class from each div as you click the arrow: 我使用此jQuery进行此工作,当您单击箭头时,该jQuery添加了一个类/从每个div中删除了一个类:

 jQuery(document).ready(function($) {
// Code that uses jQuery's $ can follow here.


$('div.strange-container').first();

$('a.display').on('click', function(e) {
e.preventDefault();

  var t = $(this).text(),
  that = $(this);

if (t === '↓' && $('.current').next('div.strange-container').length > 0) {
    var $next = $('.current').next('.strange-container');
    var top = $next.offset().top;

    $('.current').removeClass('current');

    $('html, body').animate({
      scrollTop: top     
    }, function () {
           $next.addClass('current');
    }); // not using this portion for a previous button but left it in anyway just in case
 } else if (t === 'prev' && $('.current').prev('div.strange-container').length > 0) {
    var $prev = $('.current').prev('.strange-container');
    var top = $prev.offset().top;

    $('.current').removeClass('current');

    $('body').animate({
      scrollTop: top     
    }, function () {
           $prev.addClass('current');
    });
 } 
 });

 }); 

The problem: 问题:

Once the user clicks all the way through the existing posts on the page, the arrow doesn't reset and no longer works, even if the user scrolls back up to the top of the page. 一旦用户一直单击页面上的现有帖子,即使用户向上滚动到页面顶部,箭头也不会重置并且不再起作用。 Is there another way to accomplish this that wouldn't render the down-arrow navigation useless after the user has gone all the way through the posts once? 还有另一种方法可以做到这一点,即在用户一次浏览完所有帖子之后,向下箭头导航不会变得无用吗?

Or, is there a way to hide the down-arrow navigation once the user has scrolled to the bottom of the page (or navigated through all of the posts)? 或者,一旦用户滚动到页面底部(或浏览所有帖子),有没有办法隐藏向下箭头的导航?

After many hours of looking for an appropriate solution, I stumbled on this thread: Scroll to div on click, loop around at end 在寻找合适的解决方案许多小时之后,我偶然发现了这个线程: 单击滚动到div,最后循环

I modified the fiddle, and it works perfectly for my purposes: 我修改了小提琴,它非常适合我的目的:

https://jsfiddle.net/9x335kzg/25/ https://jsfiddle.net/9x335kzg/25/

var curr_el_index = 0;
var els_length = $(".section").length;

$('.btnNext').click(function () {
  curr_el_index++;
  if (curr_el_index >= els_length) curr_el_index = 0;
$("html, body").animate({
    scrollTop: $(".section").eq(curr_el_index).offset().top - 20
  }, 700);
});

If someone wouldn't mind explaining to me how this script forces the looping when the user clicks to the end of the divs, I'd really appreciate learning more about this solution I stumbled upon. 如果有人不介意向我解释当用户单击div的末尾时此脚本如何强制循环,我将非常感谢对我偶然发现的该解决方案的更多了解。

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

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