{"id":32723,"date":"2023-02-01T09:27:52","date_gmt":"2023-02-01T03:57:52","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-jquery-issue-with-a-back-to-top-function\/"},"modified":"2023-02-01T09:27:52","modified_gmt":"2023-02-01T03:57:52","slug":"solved-jquery-issue-with-a-back-to-top-function","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-jquery-issue-with-a-back-to-top-function\/","title":{"rendered":"[Solved] JQuery issue with a back to top function"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-57113076\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"57113076\" data-parentid=\"57112970\" data-score=\"2\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>You should edit your CSS to hide your &#8220;Back to Top&#8221; button by default, and then show it when the <code>show<\/code> class is added.<\/p>\n<pre class=\"lang-css prettyprint-override\"><code>#toTop {\n    display: none;\n    background-color: #FF9800;\n    width: 50px;\n    height: 50px;\n    text-align: center;\n    border-radius: 4px;\n    margin: 30px;\n    position: fixed;\n    bottom: 30px;\n    right: 30px;\n    transition: background-color .3s;\n    z-index: 1000;\n}\n#toTop.show {\n    display: inline-block;\n}\n<\/code><\/pre>\n<\/p>\n<div class=\"snippet\" data-lang=\"js\" data-hide=\"false\" data-console=\"true\" data-babel=\"false\">\n<div class=\"snippet-code\">\n<pre class=\"snippet-code-js lang-js prettyprint-override\"><code>jQuery(document).ready(function() {\r\n    var btn = $('#toTop');\r\n\r\n    $(window).scroll(function() {\r\n        if ($(window).scrollTop() &gt; 300) {\r\n            btn.addClass('show');\r\n        } else {\r\n            btn.removeClass('show');\r\n        }\r\n    });\r\n\r\n    btn.on('click', function(e) {\r\n        e.preventDefault();\r\n        $('html, body').animate({\r\n          scrollTop: 0\r\n        }, 700);\r\n    });\r\n});<\/code><\/pre>\n<pre class=\"snippet-code-css lang-css prettyprint-override\"><code>.sectionA {\r\n  width: 100%;\r\n  height: 600px;\r\n  background-color: pink;\r\n}\r\n\r\n.sectionB {\r\n  width: 100%;\r\n  height: 600px;\r\n  background-color: green;\r\n}\r\n\r\n.sectionC {\r\n  width: 100%;\r\n  height: 600px;\r\n  background-color: purple;\r\n}\r\n\r\n.sectionD {\r\n  width: 100%;\r\n  height: 600px;\r\n  background-color: orange;\r\n}\r\n\r\n#toTop {\r\n    display: none;\r\n    background-color: #FF9800;\r\n    width: 50px;\r\n    height: 50px;\r\n    text-align: center;\r\n    border-radius: 4px;\r\n    margin: 30px;\r\n    position: fixed;\r\n    bottom: 30px;\r\n    right: 30px;\r\n    transition: background-color .3s;\r\n    z-index: 1000;\r\n}\r\n#toTop.show {\r\n    display: inline-block;\r\n}\r\n#toTop:hover {\r\n    cursor: pointer;\r\n    background-color: #333;\r\n}\r\n#toTop:active {\r\n    background-color: #555;\r\n}\r\n#toTop::after {\r\n    content: \"\\f077\";\r\n    font-family: FontAwesome;\r\n    font-weight: normal;\r\n    font-style: normal;\r\n    font-size: 2em;\r\n    line-height: 50px;\r\n    color: #fff;\r\n}<\/code><\/pre>\n<pre class=\"snippet-code-html lang-html prettyprint-override\"><code>&lt;script src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/jquery\/3.3.1\/jquery.min.js\"&gt;&lt;\/script&gt;\r\n&lt;link rel=\"stylesheet\" href=\"https:\/\/maxcdn.bootstrapcdn.com\/font-awesome\/4.5.0\/css\/font-awesome.min.css\"&gt;\r\n&lt;p style=\"position:fixed\"&gt;Scroll Down and use the toTop button&lt;\/p&gt;\r\n&lt;br&gt;\r\n&lt;p style=\"position:fixed\"&gt;\r\nAt the top, the \"Back to Top\" button does not show.\r\n&lt;b&gt;It works!&lt;\/b&gt;\r\n&lt;\/p&gt;\r\n&lt;section class=\"sectionA\"&gt;\r\n\r\n&lt;\/section&gt;\r\n&lt;section class=\"sectionB\"&gt;\r\n\r\n&lt;\/section&gt;\r\n&lt;section class=\"sectionC\"&gt;\r\n\r\n&lt;\/section&gt;\r\n&lt;section class=\"sectionD\"&gt;\r\n\r\n&lt;\/section&gt;\r\n&lt;\/body&gt;\r\n&lt;a id=\"toTop\"&gt;&lt;\/a&gt;<\/code><\/pre>\n<\/div>\n<\/div><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">0<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved JQuery issue with a back to top function <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] You should edit your CSS to hide your &#8220;Back to Top&#8221; button by default, and then show it when the show class is added. #toTop { display: none; background-color: #FF9800; width: 50px; height: 50px; text-align: center; border-radius: 4px; margin: 30px; position: fixed; bottom: 30px; right: 30px; transition: background-color .3s; z-index: 1000; } #toTop.show { &#8230; <a title=\"[Solved] JQuery issue with a back to top function\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-jquery-issue-with-a-back-to-top-function\/\" aria-label=\"More on [Solved] JQuery issue with a back to top function\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[333,388],"class_list":["post-32723","post","type-post","status-publish","format-standard","hentry","category-solved","tag-javascript","tag-jquery"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] JQuery issue with a back to top function - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-jquery-issue-with-a-back-to-top-function\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] JQuery issue with a back to top function - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] You should edit your CSS to hide your &#8220;Back to Top&#8221; button by default, and then show it when the show class is added. #toTop { display: none; background-color: #FF9800; width: 50px; height: 50px; text-align: center; border-radius: 4px; margin: 30px; position: fixed; bottom: 30px; right: 30px; transition: background-color .3s; z-index: 1000; } #toTop.show { ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-jquery-issue-with-a-back-to-top-function\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2023-02-01T03:57:52+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-jquery-issue-with-a-back-to-top-function\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-jquery-issue-with-a-back-to-top-function\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] JQuery issue with a back to top function\",\"datePublished\":\"2023-02-01T03:57:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-jquery-issue-with-a-back-to-top-function\/\"},\"wordCount\":43,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"javascript\",\"jquery\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-jquery-issue-with-a-back-to-top-function\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-jquery-issue-with-a-back-to-top-function\/\",\"name\":\"[Solved] JQuery issue with a back to top function - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2023-02-01T03:57:52+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-jquery-issue-with-a-back-to-top-function\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-jquery-issue-with-a-back-to-top-function\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-jquery-issue-with-a-back-to-top-function\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] JQuery issue with a back to top function\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] JQuery issue with a back to top function - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-jquery-issue-with-a-back-to-top-function\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] JQuery issue with a back to top function - JassWeb","og_description":"[ad_1] You should edit your CSS to hide your &#8220;Back to Top&#8221; button by default, and then show it when the show class is added. #toTop { display: none; background-color: #FF9800; width: 50px; height: 50px; text-align: center; border-radius: 4px; margin: 30px; position: fixed; bottom: 30px; right: 30px; transition: background-color .3s; z-index: 1000; } #toTop.show { ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-jquery-issue-with-a-back-to-top-function\/","og_site_name":"JassWeb","article_published_time":"2023-02-01T03:57:52+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-jquery-issue-with-a-back-to-top-function\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-jquery-issue-with-a-back-to-top-function\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] JQuery issue with a back to top function","datePublished":"2023-02-01T03:57:52+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-jquery-issue-with-a-back-to-top-function\/"},"wordCount":43,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["javascript","jquery"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-jquery-issue-with-a-back-to-top-function\/","url":"https:\/\/jassweb.com\/solved\/solved-jquery-issue-with-a-back-to-top-function\/","name":"[Solved] JQuery issue with a back to top function - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2023-02-01T03:57:52+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-jquery-issue-with-a-back-to-top-function\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-jquery-issue-with-a-back-to-top-function\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-jquery-issue-with-a-back-to-top-function\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] JQuery issue with a back to top function"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/32723","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=32723"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/32723\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=32723"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=32723"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=32723"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}