Keep element scrollable until last content is reached - javascript

I'm not sure what kind of keyword should I use to ask this question,
I already search this in Google but looks like my keyword is bad.
The problem is, I have two div relative element with dynamic content, meaning the height of this two element is not fixed and they are scrollable.
Let's say the first div has a class name of l-content stand for Left-Content and the second div has a class name of r-content stand for Right-Content.
The l-content will have more content than the r-content but they will both scrollable when the screen size is not enough to show the whole content.
Now what I want to happen is when the r-content has reached it's last content then scrolling on it will be stop so that only the l-content will continue scrolling.
The Example is like on Facebook, as you can see where ads,Recommendation,etc are shown in the right side of it
when the last content is reached then that element looks like fixing it's position.
I'm not sure if it is fixing or not but I'd like my r-content to be like that But have no idea on how.
Current this is all i have, note that I am using Bootstrap on this:
CSS
h1 {
font-family: sans-serif;
color: #FFF;
letter-spacing: 2px;
font-weight: 300;
}
.l-content, .r-content {
display: table;
float: left;
margin-bottom: 8px;
text-align: center;
padding: 20px;
}
.l-content {
width: 800px;
height: 2000px;
background: #E04C4E;
margin-right: 8px;
}
.r-content {
width: 430px;
height: 1000px;
background: #5996BC;
}
HTML
<!--LEFT CONTENT-->
<div class="col-lg-8 l-content">
<h1>Left Content with Continue Scrolling.</h1>
</div>
<!--RIGHT CONTENT-->
<div class="col-lg-4 r-content">
<h1>Right Content with Continue Scrolling but scrolling will stop when last content is reached.</h1>
</div>
I am not sure if it they are using a Javascript to do it.
Anyway, Thank you so much. Any help would be appreciated.

I believe you need javascript here, and jQuery is geared for exactly this.
scroll() and scrollTop() are the magic functions here:
var h = $(preceding-content).height()
var s = $('#your-sticky-element')
$(window).scroll(function(){
if($(window).scrollTop() <= h){
$(s).css({
'position':'fixed',
'top':'0px'
});
}else{
$(s).css({
'position':'initial'
});
}
}
This will work best if it's static content above your sticky element. If it's updating its height in real time, you're going to be doing a lot of calculations just to keep an element in place.

Related

Javascript attempt to keep sidebar at 100vh at all times

Working on a project requiring old-school CSS support and want to keep <aside> filling up the page by remaining the same height as the <article> element's box (It may be between a header and footer, in which case it would not extend past them.)
<article id="article">
...
<aside id="sidebar">
Here's my attempt at dynamically setting the height. It doesn't seem to work.
<script>
function sidebarHeight() {
sidebar=window.document.getElementById('sidebar')
article=window.document.getElementById('article')
sidebar.style.height=article.style.height;
console.log('article.style.height: ' + article.style.height);
}
window.onresize=sidebarHeight;
</script>
This doesn't work because article.style.height always has the value "" instead of the current height. How can I keep the sidebar stretched vertically in sync with the article height?
By the way, can someone explain why the only way to get at properties like height, or at least color, which I know works, requires a call to getElementById()? If I use the more logical-sounding getElementsByTagName('article') I get access to a much more limited set of properties.
To get the calculated height onresize you need to use .offsetHeight and you need to add a unit to the returned value.
sidebar.style.height = `${article.offsetHeight}px`;
also, your DOM queries should just be called on document
sidebar = document.getElementById('sidebar')
article = document.getElementById('article')
article {
float: left;
height: 40vh;
width: 75vw;
background-color: gray;
}
aside {
float: right;
width: 20vw;
background-color: tomato;
}
<article id="article"></article>
<aside id="sidebar"></aside>
<script>
function sidebarHeight() {
sidebar= document.getElementById('sidebar')
article= document.getElementById('article')
sidebar.style.height= `${article.offsetHeight}px`;
}
window.onresize=sidebarHeight;
</script>

How to center <div> inside a <button>?

I have a round < button > with a < div > inside that represents a Unicode image. Currently the button is set to border-radius: 12px; height: 24px; and width: 24px; and the < div > is to font-size: 17px. The < div > Unicode image sits inside but not centered and the button is slightly off to the side.
How can I get the < div > to center inside an oval button despite what font-size the < div > is?
EDIT
I want to create a circle/round button with an emoji center to the middle of the button despite the button's size or the emoji image's size.
CSS for the button and emoji image for div:
#emoji-button {
border-radius: 19px;
width: 38px;
height: 38px;
}
#thumb-emoji:after {
content: "\01F44C";
font-size: 20px;
}
And round/circle button with emoji image inside:
<button
type="submit"
id="emoji-button"
>
<div id="thumb-emoji"></div>
</button>
But it is not centered.
And is there a way to just back the emoji image alone to be clickable for a method?
First off:
A <div> is a block element by nature. It will always become 100% wide. If you want it to not be 100% wide, give it a display:inline-block so it won't get bigger than it needs to be. Then give it a margin:0 auto; or a text-align:center on the parent to center it.
HOWEVER, You are not allowed to put <div>s inside of <buttons>. it is invalid HTML
See this answer for more information:
Why can't a <button> element contain a <div>?
Or, you could read here, from W3 that only phrasing content is expected to be used within a button:
https://www.w3.org/TR/2012/WD-html5-20120329/the-button-element.html#the-button-element
If you do not know what phrasing content is, See this page:
https://www.w3.org/TR/2012/WD-html5-20120329/content-models.html#phrasing-content
-- if you are looking into styling buttons specifically, maybe this very short tutorial would help:
http://web.archive.org/web/20110721191046/http://particletree.com/features/rediscovering-the-button-element/
Here is a fiddle of a working button like yours:
https://jsfiddle.net/68w6m7rr/
I honestly didn't have many problems with this. I only replaced your <div> with a span, that's it.
can you post your code?
You should NOT need a div inside the button. If you need the button to have a specific style give it a class. You could do something like this
CSS:
button.something {
padding: 25px;
border-radius: 100%;
font-size: 20px;
border: none;
}
HTML:
<button class="something">👌</button>
For clean and valid code, you'd better use a :before or :after pseudo-element. This would also take care of the centering by default.
It's even easy to set the content. Either in css only, like this:
1.
button:before {content:"\25b6";}
(put your unicode value there and classes/ids as needed, then specify them in turn in css)
2.
Or if you need to specify the value in mark-up, drop a custom data-* attribute like this:
<button data-myunicode="\25b6"></button>
with each button taking it's own value, then drop this single line in css:
button:before {content:attr(data-myunicode);}
Before answering, let's clear some things out.
div is a block level element, used in an inline element, which is the button element. Browsers will consider this invalid and will fix it by removing the block element from the inline element. For more about CSS concepts like box model, box generation please refer to these resources:
https://developer.mozilla.org/en/docs/Web/HTML/Block-level_elements#Block-level_vs._inline
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Visual_formatting_model
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Box_Model/Introduction_to_the_CSS_box_model
Also, if you are using an IDE, make sure you have installed linting/hinting tools to help you out. These tools can help you in code authoring so, make sure you have them. If you are using software like VSCode or Sublime Editor, there are many free code analysis tools out there.
Let's go back to the code now.
You said
I want to create a circle/round button with an emoji center to the
middle of the button despite the button's size or the emoji image's
size.
I went ahead and created a plunk here where I demonstrate this. Essentially, I wrapped the button around a div which serves as a container and through some CSS magic, I made it to have the same height as its width. More on that you can find at this SO answer.
The #emoji-button then has a border-radius: 100% in order to be round, width is inherited from the parent, meaning it has the same as the container and it position is absolute in order to fit in the container.
The #thumb-emoji has changed to a span element. By user agent styles it has text-align:center.
<div class="button-group">
<button type="submit" id="emoji-button">
<span id="thumb-emoji"></span>
</button>
</div>
CSS:
.button-group {
position: relative;
width: 100px;
}
.button-group:before {
content: "";
display: block;
padding-top: 100%;
}
#emoji-button {
width: inherit;
border-radius: 100%;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
}
#thumb-emoji:after {
content: "\01F44C";
font-size: 200%;
}
You can change the .button-group width to whatever width you want, it will still keep its 1:1 ratio.
You can use then media queries on .button-group to adjust the font-size of your #thumb-emoji, by setting your desired breakpoints.

Possible to remove padding on higher order element from lower order element?

Bit of a weird question and this is very hacky, but I am stumped. I am using an internal tool to create a webpage. As such, I only have access to some of the generated HTML and CSS due to the nature of these tools.
So, forced upon me is the HTML
<div class="example">
<div class="whatICanAccess">
</div>
</div>
And my CSS :
.example {
padding : 1.6em;
}
The only place I can edit my CSS is within the "WhatICanAccess" HTML tag, using style="foo".
Is it possible for me to remove the padding from the outer element ("example") here in any way?
Is it possible for me to remove the padding from the outer element and ONLY have it effect the direct parent of "WhatICanAccess"?
Example is a class that is used throughout the code, and I would only like to remove it in one particular place - but as I say, I cannot add more specific identifiers/tags - I can only edit in this one place.
Can anyone help? Thank you :)
UPDATE :
I now have this HTML :
<div class="example">
<div class="moreSpecific" style=" padding: -1.8em;>
</div>
</div>
but the 1.6em of .example is still overriding. What have I done wrong here ?
Give the 1 place a separate class and give it negative margins to balance the padding.
Alternatively, use position: absolute & use width and height values.
With either you don't have to worry about not having access to the parent element.
Tried to make a JSFiddle, but apparently they've removed the "save" option for the time being ...
<div class="example">
<div class="whatICanAccess">
<div class="noPadding">This div has negative margins and absolute positioning</div>
<div>This div inherits its padding from 'example'</div>
</div>
</div>
.example {
padding : 1.6em;
height: 400px;
width 100%;
background-color: #ccc;
}
.whatICanAccess {
height: 300px;
background-color: #fff;
}
.noPadding {
position: absolute;
margin: -1.6em 0 0 0;
background-color: #eee;
}
If you must use inline CSS...
<div style="margin: -1.6em 0 0 0; position: absolute;">This div has negative margins and absolute positioning</div>
If you know the id of example, you could just use document.getElementById('example').style.padding='0px';
Giving your html a negative margin or padding might work too.

using jQuery slideUp causes "jumpy" interface

On the demo link below, I am using jQuery slideUp and you will notice after it slides up, there is a quick jump of the content.
Do you know why this is? My html is valid (with the exception of the select option not having a label attribute..which I am still figuring out...). Do I have something positioned incorrectly?
http://demo.phppointofsalestaging.com/index.php
(Click login --> Sales -->Show Item Grid THEN Hide Item Grid to see the bug)
this inline style
<div style="margin-top: 39px;" id="content">
and line 724 of unicorn.css
#content {
...
margin-top: -39px;
...
}
... are conflicting with each other.
If you remove both, the page doesn't jump.
You have set a margin-top on the content div of 39px. This is only visible when you slide down the item grid. It seems to 'jump' when sliding back up because of this margin. Try setting the margin to 0px;
<div id="content" style="margin-top:0px;">
I played around a little bit and this is being caused by the margin-top:39px on your #content div, if you remove that and use top:39px instead of margin-top:39px on the #content element instead it doesn't jerk either - but it also causes the button to jump a bit on slideUp and slideDown so you will need to tweak the css of the button wrapper area like so:
To fix the button jumping issue:
#show_hide_grid_wrapper {
position: relative;
text-align: right;
padding: 10px;
}
As prev. answers mention, you have margin-top 39px on #content. Setting it to 0 will solve the problem, but it will also remove your beautiful dark gray section above the content. To get it back, add this to your CSS:
#content:before {
content: '';
display: block;
width: 100%;
height: 39px;
background: YOUR GRAY COLOR;
}

Icon to move when hovering over a menu button

Before you read this please get up this website to see what I am trying to do:
https://www.kris-willis.com
As you can see there is a RED arrow located below the menu and what it is that I'm trying to achieve is... when I hover over a menu button the arrow moves to the same button I'm hovering over without reloading the page.
Ideally I'd like the arrow to move back to a default button.. and also for the default button to change if clicked on a different menu button.
If you know any links to examples etc... I would really appreciate it!
Thank you for your time,
Kerry x
The first thing is that you have a wrong DOCTYPE.
<!DOCTYPE html PUBLIC "">
This causes you page to load in quirk mode. Change it to
<!DOCTYPE html>
for HTML5 or use the complete one including the FSI & FPI.
Second is you are using a <table> for navigation. Nothing seriously wrong with it but people tend to use ul
For the :hover, you can simply use
#MenuPosition table tbody tr td:hover
{
background-image: url("/images/Arrow.jpg");
}
You might have to play with paddings and margins or maybe use display: block or display: inline-block to position the arrow correctly.
Make the "buttons" anchors. Using css set create a rule for :hover to set a background image that contains the arrow.
There are plenty of CSS tutorials out there, Nettuts and Webdesigntuts have a lot of navigation articles. Or if you are comfortable with emulating others, find a site you like and pick apart the source until you figure out how they did it.
Keep in mind that javascript is not at all necessary to accomplish what you are doing. Unless you want some animations, and even then CSS can handle most of that work, pure CSS in my opinion is the better approach.
PURE CSS SOLUTION
Check this answer.
Is there any way to hover over one element and affect a different element?
So it might be:
#thething {
margin: 0;
}
.classone:hover + #thething {
margin-top: 10px;
margin-left: 10px;
}
If they're adjacent siblings in a parent div.
Just move the arrow bymargin-left with respect to left of the td DEMO
$("#Arrow").css({"margin-left":$(this).position().left+($(this).width()/2)-2});
Tp do this Add jQuery libirary to the head section of your page
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
Add this code in a external js file and add it to head section of your page
$(function(){
$("#MenuPosition").on("hover","td",function(){
$("#Arrow").css({"margin-left":$(this).position().left+($(this).width()/2)-2});
});
});
EDIT : For restoring the arrow orignal position use
$(function(){
currentPos = $("#Arrow").css("margin-left");
$("#MenuPosition").on("hover","td",function(){
$("#Arrow").css({"margin-left":$(this).position().left});
});
$("#MenuPosition").on("mouseout","td",function(){
$("#Arrow").css({"margin-left":currentPos});
});
});
NOTE : PLEASE SEE THE CALCULATION PART AND CORRECT IT.
PS: cant correct is because its my log out time from office ;) . but i thing you got the logic to do it
You can do something like this:
Using a span to add the bg arrow below the nav/menu lis in the HTML:
<ul class="nav">
<li>
Menu 1
<span class="arrow"> </span>
</li>
<li>
Menu 2
<span class="arrow"> </span>
</li>
</ul>
The CSS:
.nav {
font-size: anypx;
list-style: none outside none;
margin: 0;
padding: 0;
position: relative;
}
.nav li {
background: #whatev;
display: block;
float: left;
height: anypx;
line-height: anypx;
padding: 0;
text-align: center;
}
.nav li a {
color: #any;
display: block;
padding: any;
position: relative;
text-decoration: none;
width: auto;
}
.arrow {
background: url("images/arrow.png") no-repeat scroll 0 9px transparent;
display: none;
height: anypx;
text-indent: -9999px;
width: whatevs;
z-index: 9999;
}
And Finally the JS/Jquery that makes it work:
$(document).ready(function(){
Your_menu();
});
function Your_menu(){
$(".nav li").hover(function(){
$(this).find('.arrow').css({visibility: "visible",display: "none"}).show();
},function(){
$(this).find('.arrow').css({visibility: "hidden"});
});
}
Here is a site that is showing this :)
http://www.drexelmedicine.org/

Categories

Resources