Last active
June 24, 2016 03:56
-
-
Save Jacob-Gray/eb54050765f90169abdc9b9f82055d82 to your computer and use it in GitHub Desktop.
Collapse a Stack Overflow answer if it is too long, allowing easier access to the comment, and action sections
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name SO Answer Collapse | |
// @namespace jacob-gray.github | |
// @version 0.1 | |
// @description Collapse a SO answer if it is too long | |
// @author Jacob the Gray | |
// @match http://stackoverflow.com/questions/* | |
// @include /^https?:\/\/\w*.?(stackoverflow|stackexchange|serverfault|superuser|askubuntu|stackapps)\.com\/(questions|posts|review|tools)\/(?!tagged\/|new\/).*/ | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
var styles = ".post-text:after{content:'';transition:.3s}.collapse-answer{overflow:hidden;position:relative}.collapse-answer:before{content:'Double-click to toggle collapse';position:absolute;bottom:10px;left:0;right:0;text-align:center;z-index:100;color:#2196F3;cursor:pointer}.collapse-answer:after{position:absolute;background:linear-gradient(transparent, white);top:0;left:0;width:100%;height:100%;z-index:99;cursor:pointer}", | |
style = $("<style></style>").html(styles).appendTo("body"), | |
ans = $(".answercell .post-text"); | |
var maxHeight = 700, | |
collapseHeight = 200; | |
ans.each(function(){ | |
var $this = $(this), | |
height = $this.height(); | |
if(height > maxHeight){ | |
$this.addClass("collapse-answer").animate({height:collapseHeight},300); | |
} | |
$this.on("dblclick",function(){ | |
if($this.hasClass("collapse-answer")){ | |
$this.animate({height:height},{duration:300,complete:function(){ | |
$this.removeClass("collapse-answer"); | |
}}); | |
} | |
else $this.addClass("collapse-answer").animate({height:collapseHeight},300); | |
}); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment