在WordPress中创建一个粘性的社交栏

Create a Sticky Social Bar in WordPress

本文关键字:一个 WordPress 创建      更新时间:2023-09-26

我正在为WordPress主题制作粘性社交栏。问题是div不随内容滚动。下面给出了JS、CSS和PHP的代码。

我已经在WordPress中注册了脚本function.php

wp_register_script('topnews', get_template_directory_uri() . '/js/scripts.js', array('jquery'), '', true);
wp_enqueue_script('topnews');

我想告诉你,所有其他代码在这个JS文件是完美的工作。在scripts.js中,我放置了以下代码:

function sticky_share()
{
    "use strict";
    var sharerbar = $('.article-sharer');
    var dummyblock = $('.dummy-share-block');
    var sharetop = 0;
    var sticky_share_bar = function()
    {
        var toppos = $(window).scrollTop();
        if (toppos > sharetop) {
            if ($('.article-sharer-placeholder').length > 0) $('<div class="article-sharer-placeholder"></div>').insertBefore(sharerbar);
            sharerbar.addClass('fixed');
            dummyblock.addClass('fixed');
        } else {
            $('.article-sharer-placeholder').remove();
            sharerbar.removeClass('fixed');
            dummyblock.removeClass('fixed');
        }
    };
}

PHP代码也在我的single.php文件

下面给出
    <section class="article-sharer section container clearfix">
    <div class="socials-share">
        <a target="_blank" data-shareto="Facebook" href="https://www.facebook.com/sharer/sharer.php?u=<?php echo wp_get_shortlink() ?>" class="social-share share-facebook">
            <i class="fa fa-facebook"></i><span class="share-text">Share to Facebook</span>
        </a>
        <a target="_blank" data-shareto="Twitter" href="https://twitter.com/home?status=<?php echo urlencode($posttitle . ". " . esc_url( wp_get_shortlink() )); ?>" class="social-share share-twitter">
            <i class="fa fa-twitter"></i><span class="share-text">Share to Twitter</span>
        </a>
        <a target="_blank" data-shareto="Google" href="https://plus.google.com/share?url=<?php echo wp_get_shortlink() ?>" class="social-share share-google-plus">
            <i class="fa fa-google-plus"></i>
        </a>
        <a target="_blank" data-shareto="Pinterest" href="https://pinterest.com/pin/create/button/?url=<?php echo wp_get_shortlink() ?>&amp;media=<?php echo esc_url( $featured_img ); ?>&amp;description=<?php echo urlencode( get_the_title() ); ?>" class="social-share share-pinterest">
            <i class="fa fa-pinterest"></i>
        </a>
        <a target="_blank" data-shareto="Linked In" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=<?php echo wp_get_shortlink() ?>&amp;title=<?php echo urlencode( get_the_title() ) ?>&amp;summary=<?php echo urlencode( wp_strip_all_tags( get_the_excerpt() )) ?>&amp;source=<?php echo urlencode( get_bloginfo( 'name' ) ) ?>" class="social-share share-linkedin">
            <i class="fa fa-linkedin"></i>
        </a>
        <a target="_blank" data-shareto="VK" href="http://vkontakte.ru/share.php?url=<?php echo wp_get_shortlink() ?>&amp;title=<?php echo urlencode( get_the_title() ) ?>&amp;summary=<?php echo urlencode( wp_strip_all_tags( get_the_excerpt() )) ?>&amp;source=<?php echo urlencode( get_bloginfo( 'name' ) ) ?>" class="social-share share-vk">
            <i class="fa fa-vk"></i>
        </a>
    </div>
    <div class="article-shorturl">
        <input type="text" id="shorturl" class="shorturl" data-clipboard-text="<?php echo wp_get_shortlink() ?>" value="<?php echo wp_get_shortlink() ?>">
    </div>
</section>
<div class="dummy-share-block"></div>

CSS也在下面给出,在我的style.css文件:

.article-sharer {
    padding: 0;
}
.article-sharer-placeholder {height: 160px;}
.article-sharer.fixed {
    background: #fff;
    position: fixed;
    top: 0;
    z-index: 0;
    margin-left: -1px;
    border: 1px solid #e5e5e5;
    -webkit-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.07);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.07);
}
.dummy-share-block {
    height: 61px;
    width: 100%;
    display: none;
    position: relative;
    content: " ";
}
.dummy-share-block.fixed {
    display: block;
}
.socials-share {
    padding: 10px 0 10px 30px;
    float: left;
}
.article-sharer .social-share {
    font-size: 13px;
    margin-right: 5px;
    border-radius: 3px;
    display: inline-block;
    color: #fff;
    padding: 8px 0;
    min-width: 38px;
    text-align: center;
    background: #333;
}
.social-share .fa {font-size: 16px;color: #fff;}
.social-share.share-facebook, .social-share.share-twitter {color: #fff;padding: 8px 16px;}
.social-share.share-facebook .fa, .social-share.share-twitter .fa {margin-right: 8px;}
.social-share.share-facebook { background: #2980b9; }
.social-share.share-twitter { background: #00bff0; }
.social-share.share-google-plus { background: #e74c3c; }
.social-share.share-linkedin { background: #0087c3; }
.social-share.share-reddit { background: #373737; }
.social-share.share-pinterest { background: #CA2128; }
.article-shorturl {
    float: right;
    border-left: 1px solid #e5e5e5;
    padding: 0px 30px;
    line-height: 59px;
}
.article-shorturl input {
    height: 34px;
    line-height: 34px;
    width: 160px;
} 

我只是想知道我在哪里出错了。分享按钮在帖子中正确显示,但不会随着帖子内容滚动。

请帮我解决这个问题。谢谢大家:)

添加position:fixed;.article-sharer:

.article-sharer {
    padding: 0;
    position:fixed;
}
<标题> JSFiddle