Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sticky vote button problems on Stack Overflow #277

Closed
Sir-Cumference opened this issue Apr 12, 2017 · 2 comments
Closed

Sticky vote button problems on Stack Overflow #277

Sir-Cumference opened this issue Apr 12, 2017 · 2 comments
Assignees
Milestone

Comments

@Sir-Cumference
Copy link
Collaborator

Sir-Cumference commented Apr 12, 2017

Installed Version: 2.0.29 Environment: Tampermonkey

Current Behaviour

On this question, the post will shift once you scroll down far enough for the buttons to be sticky. This is because it has over 10k upvotes. I can't really fix this without causing the vote buttons to lag (à la the MutationObserver problem), but I hopefully improved it with the code below.

Also, you can see that the buttons will be covered by the topbar:

screen shot 2017-04-08 at 12 23 08 pm

Here's the solution I wrote (though I don't have enough rep to check if it works in the review section on SO):

stickyVoteButtons: function() {
    // Description: For making the vote buttons stick to the screen as you scroll through a post
    //https://github.com/shu8/SE_OptionalFeatures/pull/14:
    //https://github.com/shu8/Stack-Overflow-Optional-Features/issues/28: Thanks @SnoringFrog for fixing this!

    stickcells();
    $(document).on('sox-new-review-post-appeared', stickcells);

    $(window).scroll(function() {
        stickcells();
    });

    function stickcells() {
        var $votecells = $('.votecell');

        $votecells.each(function() {
            var $topbar = ($('.so-header').length) ? $('.so-header') : $('.topbar'),
                topbarHeight = $topbar.outerHeight(),
                offset = $(".review-bar").outerHeight();

            if ($topbar.css('position') == 'fixed' && !sox.location.on('/review'))
                offset += topbarHeight;
            else
                offset += 5; //For some reason I have to add the bottom margin of ".review-bar"

            var $voteCell = $(this),
                $vote = $voteCell.find('.vote'),
                vcOfset = $voteCell.offset(),
                scrollTop = $(window).scrollTop(),
                realHeight, endPos;

            $voteCell.css('min-width', Math.floor($vote.width()));

            if ($vote.length && $vote.children().last().length && $voteCell.next().length) { //These values strangely alternate between existing and not existing. This if statement insures we only get their values when they exist, so no errors.
                realHeight = $vote.children(':not(:hidden, .message-dismissable)').last().offset().top + $vote.children(':not(:hidden, .message-dismissable)').last().height() - $vote.offset().top; //Get the original height; the difference between the last child and the first child's position
                endPos = $voteCell.next().offset().top + $voteCell.next().height() - 51; //I think a bit above the end of the post (where the "edit", "delete", etc. buttons lie) is a good place to stop the stickiness.
            }

            $voteCell.on('DOMNodeInserted', function() { //Fix dismissable message boxes, like "Please consider adding a comment if you think this post can be improved." when downvoting
                $vote.find('.message-dismissable').css({
                    position: "absolute",
                    "white-space": "nowrap"
                })
            })

            if (vcOfset.top + realHeight < endPos - 25 && vcOfset.top < scrollTop + offset) { //Left condition is to get rid of a sticky zone on extremely short posts. Right condition allows stickiness unless we're above the post.
                if (scrollTop + offset + realHeight < endPos) { //Allow stickiness unless we've scrolled down past the post.
                    $vote.css({
                        position: 'fixed',
                        top: offset
                    });
                } else {
                    $vote.css({
                        position: 'absolute',
                        top: endPos - realHeight //Leave the button at its bottommost position
                    });
                }
            } else {
                $vote.removeAttr('style'); //Remove any stickiness
            }
        });
    }
},

Features Enabled

["Appearance-addAuthorNameToInboxNotifications","Appearance-dragBounty","Appearance-fixedTopbar","Appearance-isQuestionHot","Appearance-markEmployees","Appearance-metaChatBlogStackExchangeButton","Appearance-metaNewQuestionAlert","Appearance-spoilerTip","Appearance-standOutDupeCloseMigrated","Appearance-unspoil","Comments-autoShowCommentImages","Comments-commentReplies","Comments-commentShortcuts","Comments-moveBounty","Comments-showCommentScores","Comments-hiddenCommentsIndicator","Editing-editReasonTooltip","Editing-enhancedEditor","Editing-titleEditDiff","Editing-inlineEditorEverywhere","Editing-downvotedPostsEditAlert","Flags-flagOutcomeTime","Flags-flagPercentages","Flags-flagPercentageBar","Sidebar-linkedToFrom","Chat-replyToOwnChatMessages","Voting-betterCSS","Voting-grayOutVotes","Voting-stickyVoteButtons","Voting-disableOwnPostVoteButtons","Extras-alwaysShowImageUploadLinkBox","Extras-parseCrossSiteLinks","Extras-quickAuthorInfo","Extras-hideCertainQuestions","Extras-showMetaReviewCount","Extras-copyCode"]
@shu8 shu8 added this to the v2.1.0 milestone Apr 23, 2017
@shu8 shu8 self-assigned this Apr 23, 2017
@shu8
Copy link
Member

shu8 commented Apr 23, 2017

@Sir-Cumference thanks again for the fix! :) We'll get this added in ASAP :)

(sorry for the late reply as well!)

shu8 pushed a commit that referenced this issue May 25, 2017
@shu8
Copy link
Member

shu8 commented May 25, 2017

@Sir-Cumference done in dev 2.0.31! :)

(sorry this took sooooo long!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants