Smooth scroll to top of page with CSS/HTML only?

前端 未结 3 1268
伪装坚强ぢ
伪装坚强ぢ 2021-02-13 05:46

I am not permitted to add in Jquery/Javascript to this site, so I was looking for any way to do a \"Back to Top\" button, and have it transition to top with CSS/HTML only....don

3条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-13 06:23

    In order to scroll to top of the page through CSS, you would have to add some kind of trigger to change CSS. Which is, AFAIK, only possible through html inline JavaScript triggers, or EventListeners in javascript.

    You can, however, jump to a point on the page through the setting of anchors (href=#the_id_of_an_element), but this is not smooth, but instead instantaneous.

    Now, I did manage to get some sort of smooth scrolling towards the top of the page, or rather the beginning of an element, but by using CSS only, this has some uneasy side-effects. Though, I'll share the code and see if it helps anyone on a start, or with any other issue.

    /*
    	KNOWN ISSUES
    	
    	- requires constant user input.
    	Either through hover or buttonpress
    		-> If an element would be triggered, they cannot be untriggered. CSS is not made for this.
    	
    	- moves the page
    	Well, what did you expect? CSS visualises the page. You can only move 
    	the page through the CSS by editing visual content and making the page smaller,
    	basically forcing the client to move the user.
    		-> this also means that elements below the element are shown in the view
    		-> it is VERY visible, especially if you look at the scrollbar
    		
    	- has a delay depending on where the user is at
    	The animation has to reach the position of the user, basically.
    		
    	At least it works on IE?
    	But honestly, just use javascript to scroll to. It's literally an one-liner
    */
    
    /* Transition types. Fairly universal available in browsers*/
    .smooth-transition {
    	-webkit-transition: all 0.5s ease;
        -moz-transition: all 0.5s ease;
        -ms-transition: all 0.5s ease;
        -o-transition: all 0.5s ease;
        transition: all 0.5s ease;
    }
    
    .slow-transition {
        -webkit-transition: all 1.5s ease;
        -moz-transition: all 1.5s ease;
        -ms-transition: all 1.5s ease;
        -o-transition: all 1.5s ease;
        transition: all 1.5s ease;
    }
    /* End of transition types */
    
    .bottom {
    	position: fixed;
    	/*Position wherever, just know that you can set sizes using these*/
    	bottom: 0px;
    	height: 40px;
    	left: 0px;
    	right: 0px;
    	
    	z-index: 10;
    }
    
    /*
    	Settings for the transitions
    	
    	On active (which is click), the animation makes sure the page is made smaller, so 
    	the top is shown again. This also causes the user to be moved upwards
    	
    	After that animation, the page is restored in size.
    */
    #move:active ~ .mover,
    #move:focus:hover ~ .mover {
    	height: 0px;
    }
    
    #move:after {
    	content: "Back to top";
    }
    
    #move:focus:after {
    	content: "Hover to go up";
    }
    
    /* 
    	The element that holds all the data
    	It's required for this element to also be at the end of the page,
    	otherwise the user will be moved upwards, but also with the end 
    	of the page in sight. (This can be tweaked with the min-height 
    	settings though. Height - height of whatever is beneath it, but
    	the user will still see the other elements)
    */
    .mover  {
    	/*
    		Min-height here is the amount of spacing between the element and 
    		the top of the HTML page. In most cases, this is 100%, but if the
    		element is contained, then this min-height trick might not work
    		
    		Just make sure that the min-height amount is the same as the visual 
    		page size, and NOT its contents or its parent.
    	*/
    	min-height: 100%;
    	/*
    		Set to your div height. Unfortunately, animations from auto to
    		0px are not supported (yet?), and there's no real good trick 
    		as a placeholder for this one. You could mess around with 
    		max-height, but remember that this can cause a delay in animation
    		and creates differences in speed that are hard to tweak or measure.
    	*/
    	height: 1400px;
    	
    	/*
    		In order to move the page, you'll have to suppress the content 
    		that will be shown outside of the element. Ergo, hide the overflow.
    	*/
    	overflow: hidden;
    }
    
    
    
    
    
    /* To make the example element look pretty*/
    #move:hover {
    	color: whitesmoke;
    	background-color: rgb(30,30,30);
    }
    
    #move {
    	background-color: rgb(50,50,50);
    	border-radius: 2px;
    	text-align: center;
    	line-height: 40px;
    	color: lightslategrey;
    	text-decoration: none;
    }
    /* 
    	End of pretty CSS
    */
    
    
    

    Welcome at the top

    Lorem ipsum dolor sit amet, movet soleat nullam an mei, ne mei ullum errem viris. Mea et esse nullam. No sit accumsan tacimates evertitur. Et sea mutat postea. Summo numquam eu vix, vix populo denique suscipit no. Laudem elaboraret ei cum. Cetero quaeque oporteat pri in, pro simul persius an, mei in quis iriure molestie. Vix simul sententiae suscipiantur in, qui errem phaedrum mediocrem ex, ei mea commune interpretaris. Id ius sumo choro accumsan. No qui facete fuisset noluisse, cum meis periculis hendrerit ut. His no quem malis minimum, veri posse abhorreant te eos, no ignota aperiam vocibus mei. Pro ex phaedrum maiestatis, an mei appareat adversarium. Pro ne atomorum consequuntur, velit viderer ei eam, vim luptatum adolescens id. Lucilius periculis ex eos, nullam laboramus his ex. Ex eius deseruisse persequeris eos, ut mea enim mutat efficiendi, nam magna rebum labore ad. Ut verear maiorum nam, per an nibh maluisset. Quas viderer no vix, ut tantas molestiae aliquando vel, ne eam nibh mandamus. Mel ad sensibus tractatos, vim cu timeam explicari. Te duo choro option labores, ius principes ullamcorper cu. Ei vis alienum corrumpit, ea has vero volutpat. Sit lobortis percipitur scripserit eu. Cu mea putent detraxit, mel purto eruditi contentiones in, et urbanitas honestatis sit. Quod numquam accusata et vim. Discere expetenda in per. Sit eu dolor persius, virtute repudiandae eum et. Ne vix lobortis constituam, cum dicant iisque hendrerit cu. Scribentur efficiantur ex sed, pri ex congue tollit lobortis, vix affert aliquip tractatos at. Hinc esse legimus sit at, accusata hendrerit expetendis ex eum. Vide civibus sed at, et has ridens dictas noluisse. His philosophia comprehensam cu, omnium abhorreant usu cu, ne quo vero eius consetetur. In cum nusquam pericula. Luptatum electram nec et, purto perpetua pri eu, an pri minim adolescens. Ex vel causae omittam, te vis semper patrioque, nec prima quaestio an. Ferri admodum definitionem est no, eu etiam tritani scriptorem eum. Nam cu viris dictas eleifend, has an idque abhorreant, sed id choro inciderint reprehendunt. Pri putant aliquid intellegat at. Cibo discere at sed, id elitr nonumy has. Cu nam magna similique. Ex duis debet discere mei. An pri partem delenit mnesarchum, ut nam honestatis reformidans. Cum no dicta verterem referrentur, sit ad dolore laboramus, docendi adipisci ad eos. Porro voluptaria mea at, feugait mnesarchum usu no. Essent nostro vituperata eam ut, dicat nemore maiestatis id est. In nec solum volumus detracto, ne vis scriptorem delicatissimi. Ea saepe similique vel. Et bonorum molestiae sed, legere latine detracto duo at, per te alterum signiferumque. Novum regione civibus at pri, sit no debet phaedrum. Vis elitr nominati id, his justo soleat te, cum ut modus iusto qualisque. Nostro intellegat mei ei. Ea quo eleifend vituperata, sea at persius molestie pericula, ut vis melius molestie. Dolorem corrumpit vix ut, qualisque definiebas ea mea. Cu quo solet cetero posidonium, ut sed erat utroque, est nostro explicari euripidis eu. Oratio deserunt his ad, verterem complectitur conclusionemque ne quo, vim imperdiet complectitur te. No invenire gloriatur has. Legimus accumsan eu mea. Cu pri nonumes eleifend salutandi. Qui ei fugit cetero. Vis convenire interpretaris ex, cu vel patrioque assueverit dissentiet. Clita commune perpetua vel ex, te salutandi liberavisse interpretaris vix, te vide dictas invidunt mel. Mel id qualisque conclusionemque. Prima fabellas dignissim vim id, cum at elit luptatum concludaturque. Vix ex modus dolores voluptua. No vis velit soluta nonumes, erant officiis tractatos vix id. In nam justo delicata, ridens sadipscing per eu. Vel solet melius in. At novum inermis atomorum est, ius no malorum nominati, no consulatu interpretaris mediocritatem eos. At quis integre liberavisse eam. Modo eirmod ius ea. Posse omnes interpretaris cu cum. Ad audire vivendo quo. Et sed zril vidisse. Quo deleniti electram et, ex has accusam detraxit elaboraret. Instructior deterruisset qui at. Dictas aeterno epicuri id cum, his nostro labores comprehensam ei. Nam cu vitae facilisis, no est scripta alienum tincidunt. Duo adhuc mazim lucilius at. Cu tacimates facilisis sed. Ne rationibus referrentur pro, aliquip omittam id eam. Nec atqui noster persequeris ea. An clita dicunt sed, mea cu facer nulla prompta, ancillae fabellas vel eu. Mei ei utroque eligendi torquatos, nam ut omnes integre adversarium, mea ad omnes euripidis. Essent invenire cu nec, sit cu commodo efficiendi. Ex sed dico causae. Wisi urbanitas cu eum. Laboramus efficiendi no sed, justo latine albucius ad eum. Pro eros euripidis id. Sit cetero aliquam delicata an, duo cu tota delenit. Quo regione reformidans an, in dicit antiopam qui, omnis percipitur eum ad. Esse unum moderatius quo an, sanctus imperdiet elaboraret pro ex. Per fuisset mentitum ex. Ea pri saepe evertitur disputando. Audire maiorum consetetur no pri, his an eirmod honestatis. Pro case mazim in, eos no esse graeco causae, quo liber prodesset deterruisset at. Cu agam ceteros reprehendunt pri. Pro ne agam fugit, assum putent te nam. Cum libris facilisi eu, suas nemore cu sed. Id option vivendum pro. In duo soleat ceteros detracto, qui cu quem officiis, eam eu solet audire dissentiunt. Te vis dicunt civibus placerat, quem cetero ne vix. Fierent periculis at mel, viris eirmod constituam in eum. Cum diam consectetuer ea. Duo purto pertinax petentium in, ex veri atomorum consetetur pro. Ne eos equidem dolorem epicuri, sed prima consulatu in. Erat invidunt legendos ex duo. Ius suas principes dissentias an, eu vel dicat adipisci ocurreret. Ut summo dictas appellantur vis. Nec ad nobis tritani molestiae. Ei vis erant corrumpit comprehensam. Has ad vero quaeque corpora, volumus ullamcorper eos ut. Saepe explicari per in, ceteros iracundia vituperata ea eos. Duo cu autem partem facilisi, eum at munere complectitur. Cu nam virtute fabulas graecis, graece complectitur in est. Cum quot imperdiet ut. Et est consul dolorem theophrastus. Mazim delicatissimi eos ad. No quo dicat insolens consetetur, idque aliquando vis in, his et minimum deleniti. In modus petentium eum, id graece everti vim. Ut vocibus praesent est, cu pri modo vivendo consetetur, inani vocent antiopam usu et. Ne est recusabo democritum, dicat melius noluisse ex sea. Mea ad posse atomorum, in sed facilis concludaturque. No veri sonet mel. Ornatus delenit an qui, usu te saepe utinam vivendum, idque urbanitas pro ex. Quo ludus erroribus ea. Ea saperet atomorum honestatis sit. Eu essent impedit deseruisse ius, ex dicit mandamus his, eu mel prompta qualisque necessitatibus. Pri solet probatus at. Quo sint alienum ne. Reque malis sonet id mel, has enim mundi alienum.

    Make sure to read the comments I added in the CSS so you understand what the issues are and what everything in the file means. Though, I'm fairly sure you will nearly instantly notice one of the nasty side-effects.

提交回复
热议问题