I've created an anchor tag as follows
Top
And created following tags
<h2 id="top">Header</h2>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
when an unknown printer took a galley of type and scrambled it to make a type
specimen book. It has survived not only five centuries, but also the leap into
electronic typesetting, remaining essentially unchanged. It was popularised in the
1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more
recently with desktop publishing software like Aldus PageMaker including versions
of Lorem Ipsum.
</p>
Once clicking on a link, it's scrolling to that target but not can't see the <h2> tag. So atleast needed to scroll above the target by 10px or 20px. Please help me overcome this and the work would be more appreciable.
Do like that: add tabindex in
<h2 id="top" tabindex="1" >Header</h2>
and use focus()
$("#top").attr("tabindex",-1).focus();
OR
or the simplest way is that add a anchor tag near h2 and add id="top"
<a id="top"></a><h2>Header</h2>
but I won't prefer this.
First thing you need to change href of anchor tag as below
Top
then you can write below jQuery code to get scroll to the target
(function() {
$('a').click(function() {
$('html, body').animate({
scrollTop: $("#top").offset().top
}, 3000);
});
});
It will work.
Personally I usually minus some pixels from the top of the div so my headings appear normal, do this:
jQuery('html, body').animate({
scrollTop: jQuery( jQuery(this).attr('href') ).offset().top - 25
/* You can minus the pixels */
}, 500);
Related
I'd like to know if it's possible to auto-scroll when I'm clicking on my nav bar title right to a specific part of my page? Is it possible without using jQuery?
Very much possible without the use of Javascript or Jquery. I've created a basic version of the behavior you're looking for. This is possible using ids in your HTML. Please see the example.
a {
font-size: 10rem;
}
html {
scroll-behavior: smooth;
}
<div class="container">
<!-- potential navbar component -->
About<br>
<img src="https://dummyimage.com/1500/000/fff&text=I'm+a+dumb+image+simply+for+scroll+behavior.">
<h1 id="about">About</h1> <!-- goes to this spot of page because of id -->
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<img src="https://dummyimage.com/1500/000/fff&text=I'm+a+dumb+image+simply+for+scroll+behavior.">
</div>
Hi there i have iframe in which there is a div and inside div there is one span
<iframe id="rFrame">
<div id="resarcbTabBox">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus <span id="highLightYellow">PageMaker</span> including versions of Lorem Ipsum
</div>
</iframe>
The content inside div is huge, but i pasted just small. The iframe is vertically scrollable, what i am trying to do is, i just want to scroll to the <span> position.
I am using jquery.scrollTo.min.js.
So far i tried various code but no luck, here are some of my effort, but not luck
$("#rFrame").scrollTo("#highLightYellow", 10);
$('#rFrame').contents().find('#resarcbTabBox').scrollTo("span#highLightYellow", 10);
will anybody put me on right track, what mistake i am making.
Thanks in advance.
Fiddle : https://jsfiddle.net/vivekSalve/orLvgf8n/2/
I have a div and want to have it's background color filled based the scroll position from top. Below is my code -
HTML
<div> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. </div>
<div id='Div'>
</div>
<div> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</div>
CSS
#Div {
background-color: red;
height: 20px;
width: 0%;
}
J-query
$(window).scroll(function(){;
w = Math.floor( $(window).scrollTop() );
if (w > 5) {
$('#Div').animate({ width: '100%' }, 1000);
} else {
$('#Div').animate({ width: '0%' }, 1000);
}
});
This is running well, but I found there is a delay in the response time. Filling is not happening quickly based on the cursor position as I need to wait for few seconds to see the filling starts.
Fiddle - http://jsfiddle.net/bogaso/5r3afz7n/11/
Is there any way to make the response time instantaneous?
Any help will be highly appreciated.
Concept
Define two named animations using css #keyframes rules, one to expand the color bar, one to shrink it. Contingent on the scroll position, start the proper animation. In order to select the right one and to restrict the animation start on crossing the scroll offset threshold, a css class colorized is introduced to record state ( in this case: scroll offset > 5 ). Only if the state has changed, an animation is started. The combination of current scroll offset and presence/absence of class colorize determine which animation to select.
Jquery is not needed to manage the animation, the DOM API does suffice.
See it in action at this fiddle.
Code
HTML
<div><!-- ... long text .....></div>
<div id='Div'>
</div>
<div><!-- ... even longer text .....></div>
CSS
#Div {
background-color: red;
height: 20px;
width: 0%;
}
#keyframes colorize {
from { width: 0%; }
to { width: 100%; }
}
#keyframes decolorize {
from { width: 100%; }
to { width: 0%; }
}
JS
$(window).scroll(function(){
let w = Math.floor( $(window).scrollTop() );
let e = document.querySelector('#Div');
if (w > 5) {
if (! e.classList.contains("colorized")) {
// Adjust the scrolling state proxy
e.classList.add("colorized");
// Animation name. Links the code to the CSS spec of start/end values of the animated attribute
e.style.setProperty ( 'animation-name', 'colorize' );
// Animation starts immediately
e.style.setProperty ( 'animation-delay', '0s' );
// It takes that long for the complete animation to complete.
e.style.setProperty ( 'animation-duration', '2s' );
// The final state of the animated attribute persists.
e.style.setProperty ( 'animation-fill-mode', 'forwards' );
// The animation is run exactly once for each triggering event
e.style.setProperty ( 'animation-iteration-count', '1' );
}
} else {
if (e.classList.contains("colorized")) {
e.classList.remove("colorized");
e.style.setProperty ( 'animation-name', 'decolorize' );
e.style.setProperty ( 'animation-delay', '0s' );
e.style.setProperty ( 'animation-duration', '2s' );
e.style.setProperty ( 'animation-fill-mode', 'forwards' );
e.style.setProperty ( 'animation-iteration-count', '1' );
}
}
});
Extension
In the version shown the colored bar jumps to full width when the decolorize animation starts. Visually this is not appealing if the inverse colorize animation is in progress. A more sophisticated solution would read the current width of the color bar div and set the start value of the respective #keyframe animation accordingly.
See this code :
html{position:relative;min-height:100%;}
and
<div style="width:100%;height:100%;overflow:auto;">
<div id="main" style="width:200px;max-height:100px;">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>
</div>
In addition, Textarea scrollbar and resize grabber cannot hiding too.
How can I fix it using CSS?
in Chrome(MacOS). But Safari works well.
This code is example for quick understanding.
The contents are large enough to require scrollbar.
UPDATE : https://jsfiddle.net/xu3q4m4w/9/
I guess This problem is relates to LINK.
height set into 100% create scroll bar. Try to fix into max-height:100%. as well as width. you should set into width:100% or max-width:100% to get full width, not in pixel.
UPDATED:
<div style="width:100%;height:100%;overflow:auto;">
<div id="main" style="max-width:1500px;height:200px;">
</div>
</div>
jsfiddle
I figure out this problem. It Happen by only old chrome version (48.0.2564.97)
Now fix it. (48.0.2564.103 )
Thank you for answer.
use this fiddle overflow-x:hidden
<div style="width:100%;height:100%;overflow:auto;overflow-x:hidden;">
<div id="main" style="width:1500px;height:200px;">
</div>
</div>
Link
I have implemented a section with scroll bar which will display the description. its working fine now. but its having the default style now. i want to custom style the scroll bar so that it will look good.
HTML CODE
<div class="desc" style="width:100px; height:300px; overflow: auto; background:#CCC;" >
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>
You can use slim scroll plugin for this http://rocha.la/jQuery-slimScroll.
CSS scrollbar properties are available to customize it. E.g.:
scrollbar-3dlight-color
scrollbar-arrow-color
scrollbar-base-color
scrollbar-darkshadow-color
scrollbar-face-color
scrollbar-highlight-color
Detailed answers is available at:
CSS customized scroll bar in div