jQuery - set calculated div width - javascript

I tried to set on my page posts as cards to one div with id="content" (like on G+).
#content {
margin-top: 120px;
margin-bottom: 70px;
margin-left: auto ;
margin-right: auto ;
height: auto;
}
.card {
height: 250px;
width: 500px;
margin: 10px;
float: left;
}
and I want to calculate how much .card I can fit into the screen.
So I tried this:
$(document).ready(function () {
"use strict";
var cardAmount = (Math.floor($(window).width/520))*520;
$("#content").css("width", cardAmount);
});
but the problem is(I think) the second parameter of .css must by string and it's not.

The problem is most likely that you need to add a unit to this number so the CSS engine knows what to do with it.
Also, there's another problem with your code - your not calling width function here: $(window).width. Please remember that width is a function from jQuery API, not DOM API parameter that you can simply get like this. So the whole fix is quite simple:
var cardAmount = Math.floor($(window).width() / 520);
$("#content").css("width", cardAmount + "px");
Here - see how it works in this fiddle

I guess you are missing the syntax with the px or % in the css .
$("#content").css("width", cardAmount+"%");#or
$("#content").css("width", cardAmount+"px");#or
$("#content").css("width", cardAmount+"em");

Related

jQuery: using a div embedded in a JavaScript variable as a jQuery selector

I have a jQuery custom scrollbar, and I invoke it like this:
<script>
(function($){
$(window).on("load",function(){
$(".main_text,#C2,.png_container").mCustomScrollbar();
});
})(jQuery);
That works correctly for all of the page elements except .png_container, but unlike the other sections, that section is only used in a JavaScript variable that is used to substitute text in a placeholder ID, and I think that's where the problem is.
Here is how it's called from an "onclick" button event:
<div class="main_text">
<div id="C2">Main Text</div>
</div>
if (type == 101) {
var X = "<header>First Section</header><br>A classic example of good form/<br><br>More information<ul type=\"circle\"><li>Element Point 1<br></li><li>Element Point 1</li></ul><i><span class=\"span_01\">So much better</i></span><br><br><div class=\"png_container\"><img class=\"png_format\" src=\"images/Element 001.png\"></div>"}
document.querySelector("#C2").innerHTML = X;}
The png_container has a separate set of scroll bars, but they are not replaced by the custom scroll bars (the other page sections do get the custom scroll bars).
Here is the relevant css:
.png_container{
overflow: auto;
overflow-y: auto;
overflow-x: auto;
height: 400px;
width: 800px;
border: 2px solid;
border-color: green;
}
#C2{
color:#DBDBDB;
font-family: camphorW04-Thin,calibri,arial;
font-size: 14pt;
text-indent: 0px;
width: auto;
margin: auto;
margin-left: 20px;
margin-right: 250px;
}
So my question is: how can I replace the scroll bars on a section that is embedded in a JavaScript variable, as shown above?
My research has found some similar questions, but none that answer this specific question, so I hope somebody knows the answer. Thanks very much for any ideas.
You initialize the mCustomScrollbar plugin on load this way:
$(window).on("load",function(){
$(".main_text,#C2,.png_container").mCustomScrollbar();
});
The two first selectors have matching elements at this moment. But there is no existing element to match the last selector since .png_container is appended on click.
So you can safely remove .png_container from the load handler...
And initialise mCustomScrollbar on .png_container when it exists.
$(window).on("load",function(){
$(".main_text,#C2").mCustomScrollbar(); // Remove .png_container
});
$(".something").on("click",function(){
if (type == 101) {
var X = "<header>First Section</header><br>A classic example of good form/<br><br>More information<ul type=\"circle\"><li>Element Point 1<br></li><li>Element Point 1</li></ul><i><span class=\"span_01\">So much better</i></span><br><br><div class=\"png_container\"><img class=\"png_format\" src=\"images/Element 001.png\"></div>"}
document.querySelector("#C2").innerHTML = X;
$(".png_container").mCustomScrollbar(); // Add this.
}

Javascript issue with id and class combination within R shiny

I am working on a project and am stuck with one of my final steps. I am developing a shinyApp and have done stuff like the following within my ui.R file, at the end of my tags$body section:
tags$script(HTML('
window.onscroll = function() {myFunction()};
var sidebar = document.getElementById("sidebar");
var title = document.getElementById("title");
var titleHeight = title.offsetHeight;
function myFunction() {
if (window.pageYOffset >= titleHeight) {
if ($(window).width() > 1280) {
sidebar.classList.add("sticky-wide");
} else if ($(window).width() > 780) {
// This is the final issue:
sidebar.classList.add("sticky");
//sidebarWell.classList.add("stickyWell");
} else {
sidebar.classList.add("sticky-narrow");
}
} else {
if ($(window).width() > 1280) {
sidebar.classList.remove("sticky-wide");
} else if ($(window).width() > 780) {
sidebar.classList.remove("sticky");
//sidebarWell.classList.remove("stickyWell");
} else {
sidebar.classList.remove("sticky-narrow");
}
}
}'))...
Note that "sidebar" is the id name given to my shiny element "sidebarPanel()". So that when I scroll beyond the title at the top of the page, my sidebar becomes sticky based on the following code in my css file (note the very last element I am pretty sure is from my predecessor's previous code and I believe is some global style options assigned to a class that shiny labels automatically):
.stickyWell {
box-sizing: border-box;
width: calc(33% - 30px);
}
.sticky {
box-sizing: border-box;
width: calc(33% - 30px);
/*width: inherit;*/
position: fixed;
top: 0;
height: 100vh;
}
.sticky-narrow {
width: 100%;
}
.sticky-wide {
box-sizing: border-box;
width: calc(426.67px - 30px);
position: fixed;
top: 0;
height: 100vh;
}
/*Change style options of a singular selectInput box by first defining a class for it:*/
.my_class .selectize-input {
border-top-left-radius: 0px;
border-top-right-radius: 0px;
}
#ss-connect-dialog {
opacity: 1 !important;
padding: 1em;
position: fixed;
top: 50px;
bottom: auto;
left: 50px;
padding-left: 45px;
padding-right: 18px;
width: 300px;
height: auto;
z-index: 99999;
background-color: #404040;
color: white;
border-radius: 3px;
font-size: 0.9em;
box-shadow: rgba(0, 0, 0, 0.3) 3px 3px 10px;
}
Update: Note the if statements have been updated and now work properly. This is the closest I have gotten the code to working, but the second case in the nested if statement does not adjust the width properly.
The following was one solution I used in the past but seems incompatible with the window.onscroll feature:
#media only screen and (max-width: 1280px) and (min-width: 780px) {
#sidebar.well {
box-sizing: border-box;
width: calc(33% - 30px);
/*position: fixed;*/
overflow-y: scroll;
height: 80vh;
}
}
This used to be in my CSS file and adjusted the width the appropriate amount (note the 33% is due to column = 4 for sidebarPanel and the 30px is twice the border of the sidebarPanel, set by default, later I can make this more robust). So ideally I need to figure out a way to implement this in my javascript section of ui.R (one of my attempts being connected to the commented lines involving "stickyWell" in the first code bluck, which calls a similar looking thing, to the #media above, in the first block of css code).
Remaining portions of original post follows and can be ignored
The issue is that the width of my sidebar becomes too large when scrolling down the page. I found a solution to this earlier, but it runs into the issue of the sidebar width changing before and after I scroll beyond the title.
One way I figured out to get around this was to use:
#sidebar.well {
box-sizing: border-box;
width: calc(33% - 30px);
}
in my css file as well (where the class name "well" is assigned automatically by R shiny and specifically using the id-class name combo correctly sizes things). But this only fixes the width after scrolling beyond the title (when the sidebar gets the position:fixed property) and screws up the width prior to scrolling beyond the title.
So now what is left to do is to only enable the above css snippet when I scroll beyond the title. I have tried such things as:
var sidebarWell = document.getElementById("sidebar").getElementsByClass("well")[0];
and then within the proper if statement (in the first block of code) I tried adding things like:
sidebarWell.setAttribute("style", "box-sizing: border-box;");
sidebarWell.setAttribute("style", "width: calc(33% - 30px);");
or
sidebarWell.classList.add("stickyWell");
After first renaming "#sidebar.well{}" in the css file to "stickyWell{}".
I am new to javascript, what am I missing or doing wrong? Please note that I think I am very limited with id and class names because of how shiny labels things, and I have tried applying the above style changes to just id="sidebar" but it does not work how I think it should.
In response to the first comment:
Firstly, I figured I would include any relevant parts of the code that may create conflicts. First the way the sidebarPanel is defined:
sidebarLayout(
sidebarPanel(id="sidebar",
width=4,
...
with nothing else major in that section of ui.R (mainPanel is defined with width=8. Then in the beginning of ui.R, I have
shinyUI(fluidPage(
includeCSS("extrahtmlfunc.css"),
tags$head(
# 2018-06-19: Fixes issue where a slider that appears at certain zoom levels for figures using splitLayout (only shows up on Chrome):
tags$style(HTML(".shiny-split-layout > div { overflow: visible; }")),
# The following makes it so the UI doesn't look to weird in super-wide browsers
tags$style(type="text/css",
".container-fluid { max-width: 1280px; }"
),
...
That should be all that is relevant, the rest is minor color styles or similar tweaks. Note that this last thing is what makes some easier fixes for my solution more difficult, because I need separate cases for different page widths (I am updating the first block of code to reveal the full details of these cases in the function myFunction(), which I removed to shorten my post; note the second case of the nested if statement is the only one not working properly).
I also updated the second block of code with the rest of my css file as per request.
Found a workaround:
First off, I was able to incorporate my previous css code that fixed the issue but I was previously unable to incorporate with the window.onscroll feature. This was one of the main ways to solve my question, and there are likely other solutions which may be better.
Recall the snippet from my original CSS file that gave the correct sizing:
#media only screen and (max-width: 1280px) and (min-width: 780px) {
#sidebar.well {
box-sizing: border-box;
width: calc(33% - 30px);
/*position: fixed;*/
overflow-y: scroll;
height: 80vh;
}
}
Note that the #media... connects to the second case of the nested if statement in my javascript portion of my ui.R. It was hard to tell if I was able to replicate this with things like:
.stickyWell {
box-sizing: border-box;
width: calc(33% - 30px);
}
in conjunction with
var sidebar = document.getElementById("sidebar");
var sidebarWell = sidebar.getElementsByClassName("well")[0];
if(...){
if(...) {
...
} else if (...) {
...
sidebarWell.classList.add("stickyWell");
}...
because the "33%" seemed to not work within javascript (perhaps someone knows why). I even found and tried a way to transform the first block of CSS code directly into my javascript section:
$("#sidebar.well").css("box-sizing", "border-box");
$("#sidebar.well").css("width", "calc(33% - 30px)");
but this did not work (I even inspected the element when running the app and it did not accept the second portion of the style above).
However, once learning how to implement css code directly into javascript, I realized I might be able to replace the 33% with a precise number of pixels as that worked in the ".sticky-wide" portion of my original question. Here is the resulting solution:
shinyUI(fluidPage(
includeCSS("extrahtmlfunc.css"),
tags$head(
... ,
tags$script(HTML('
window.onscroll = function() {myFunction()};
var sidebar = document.getElementById("sidebar");
var title = document.getElementById("title");
var titleHeight = title.offsetHeight;
function myFunction() {
if (window.pageYOffset >= titleHeight) {
if ($(window).width() > 1280) {
sidebar.classList.add("sticky-wide");
} else if ($(window).width() > 780) {
sidebar.classList.add("sticky");
$("#sidebar.well").css("box-sizing", "border-box");
$("#sidebar.well").css("width", "calc(" + $(window).width()/3 + "px - 30px)");
} else {
sidebar.classList.add("sticky-narrow");
}
} else {
if ($(window).width() > 1280) {
sidebar.classList.remove("sticky-wide");
} else if ($(window).width() > 780) {
sidebar.classList.remove("sticky");
$("#sidebar.well").css("box-sizing", "");
$("#sidebar.well").css("width", "");
} else {
sidebar.classList.remove("sticky-narrow");
}
}
}
'
)
) # Closes tag$script
) # Closes tag$head
... # Rest of ui.R
)
) # Closes shinyUI
I hope this ends up helping someone else.

Get size of an unknown image and change class

On my Tumblr Theme I want to use a different CSS class for portrait images.
Images, by default, come in via img-tag and get's the class bilder. But if it's an portrait image, I would like to replace the builder class with the hhcoch class.
HTML:
<img src="{..}" />
JavaScript:
var someImg = $("img");
if (someImg.height() > someImg.width()){
$( "img" ).addClass( "hhoch" );
}
CSS:
.bilder {
height: auto; /*override the width below*/
min-width: 100%;
max-width: 100%;
display: inline;
text-align: center;
margin-left: auto;
margin-right: auto;
}
.hhoch {
min-height: 800px;
max-height: 800px;
width: auto;
}
Sum up:
The image should get a different class if it's portrait.
You've got the right idea, however that specific jQuery selector you're using is going to return an array of ALL the image tags in your document.
This means you'll want to loop over the array of image DOM nodes and apply your class that way.
var $images = $('.bilder');
$images.each(function(index, img){
if(img.height > img.width){ // is it a portrait-image?
$(img).removeClass('bilder'); // Remove default class
$(img).addClass('hhoch'); // Add the portrait class
}
});
Have you tried condensing the solution to this:
$("img.bilder").each(function(){
if (this.height > this.width) {
$(this).removeClass("bilder").addClass("hhoch");
}
});
Because I don't think all those extra lines of code are needed and might be your problem. As others were saying, it will return an array of objects so you need to loop through them all.

Divide elements width depending on amount of elements

As the title say "Divide elements width depending on amount of elements"
I want to have an progress-like bar where I can add elements and depending on the amount elements I have the progress bar would split.
This is fixed values now (33%, 33% and 34%) and I want them to change depending of how many elements I use.
Like if I have 4 elements in my list I want it to automatically divide equally with 4. Is there any easy way to do it? Maybe using only CSS? I don't mind javascript, but Im just saying that it could be something I've missed :)
Edit:
I created 3 div's and gave them all different classes.
<section class="progress-part-list">
<div class="progress-part-left">
</div>
<div class="progress-part-right">
</div>
<div class="progress-part-mid">
</div>
</section>
In my CSS with my fixed values it is:
.progress-part-mid
{
height: 100%;
width: 34%;
background-color: #52ff00;
opacity: 0.9;
display: block;
float:left;
}
.progress-part-left
{
height: 100%;
width: 33%;
background-color: red;
opacity: 0.9;
display: block;
border-top-left-radius: 50px;
border-bottom-left-radius: 50px;
float:left
}
.progress-part-right
{
height: 100%;
width: 33%;
background-color: yellow;
opacity: 0.9;
display: block;
border-top-right-radius: 50px;
border-bottom-right-radius: 50px;
float:right;
}
Are you looking for something like this?
http://jsfiddle.net/FK7N5/
You want to select all the elements, and use that to calculate the percentage.
It doesn't matter how many elements you include in your container. Just make sure they all have a common class like "progress". Remember, you can have more than one class on an element, so you can have <div class="progress progress-first"> or <div class="progress progress-middle">.
The jQuery to make it work:
// Get all parts of the progress bar.
var bars = $('.bar');
// With each one, calculate the percentage and apply the width.
bars.each(function()
{
$(this).css('width', (100 / bars.length) + '%');
});
As a person who uses javascript more than I should, the first thing that came to mind is to run some JavaScript on load, and then it can go though the 'child' property of the surrounding element (the one with class progress-part-list), then divide 100 by that, then set the styles to that.
Here's some code that would do this:
<script type="text/javascript">
var divd=100/(document.getElementsByClassName('progress-part-list')[0].children.length);
[].forEach.call(document.getElementsByClassName('progress-part-list')[0].children,function(curChild){
curChild.style.width=divd+'%';
});
</script>
Put that just after the closing tag of the <section>
That's very crude, though, you might want to fix it to make it more elegant (like modifying class styles instead of inline styles, going through all instead of just the first progress-part-list, etc
It's also untested
Try giving each of the inside-elements a margin-right of -4px and use the following JS/jQuery:
$(document).ready(function(){
$('div div').width($('div:first-child').width() / 3);
});
Here's a fiddle: http://jsfiddle.net/JthgV/
Is that what you're looking for?
P.S. Forgive the inline style. I did it that way for the sake of saving time.

How to resize a div to clients viewport height?

Ok, so i want to have a series of divs which are the exact width and height of the user's browser window, regardless of the screen size. I can easily make the divs stretch horizontally with "width: 100%;" but i cant work out how to make the height stretch itself. I am guessing that i need to use some bit of javascript to judge the height, and then another piece to resize the seperate divs. Unfortunately I am a complete javascript n00b and after two hours of seemingly fruitless searching and coming up with about 100 "solutions" this was as far as id gotten (Im sure that at some point I have probably been closer to the answer):
var viewportHeight = "height:" + document.documentElement.clientHeight;
getElementById('section-1').setAttribute('style', viewportHeight);
<div class="section" id="section-1"></div>
<div class="section" id="section-2"></div>
<div class="section" id="section-3"></div>
edit:
ah i should be more clear, im attempting to have all three divs take up the entire screen, so you have to scroll down to see each one - almost like seperate slides. The idea is that each one takes up the entire screen so you cant see the next section until you scroll down, rather than having three divs which take up a third of the screen.
If you haven't already tried it, you'll want to look at parent:child inheritance of elements within the DOM by way of using CSS.
What I want to STRESS is that everyone giving you JS hacks to accomplish this is not only providing you with overkill (YOU did ask for a JavaScript solution, so they gave it to you!), but it's also a deviation from standards. HTML is for structure, CSS is for presentation, and JavaScript is for behavioral aspects... setting a div to the width of the viewport on load is a PRESENTATION aspect and should be done in CSS... not JavaScript. If you were trying to change the width based on events or user interaction, then yes JavaScript is your friend... but stick with just HTML and CSS for now.
The trick is that most elements have an undefined height - and height is later defined by the content that the element holds.
If you want to 'trick' an element into having a height other than what it wants to default to, you'll have to explicitly define it. Since you want to inherit your height from the viewport, you'll have to define the height at the top and bring it down...
You might be in luck and can avoid JavaScript altogether (unnecessary). Just use CSS.
Try something like:
html, body {
height: 100%;
width: 100%;
}
Now, when you try to set your div's later on, specify width: 100% and the height gets inherited from the html --> body --> div.
Try that and see if that solves your problem - if not, point us to a website, a pastebin, or a SOMETHING with code in it that we can just show you how to do it (whereas what you posted for code was an attempt in JavaScript which is only 1 part of the code - post the full thing either to a server or temp site like pastebin).
Here is some sample code I wrote (tested in Chromium):
The HTML:
<html>
<head>
<title>Test Divs at 100%</title>
<link rel="stylesheet" type="text/css" href="divtest.css"
</head>
<body>
<div class="test1">aef</div>
<div class="test2">aef</div>
<div class="test3">aef</div>
</body>
</html>
The CSS:
html, body {
width: 100%;
height: 100%;
background-color: #793434;
padding: 0;
margin: 0;
}
div {
height: 100%;
width: 100%;
}
.test1 {
background-color: #E3C42E;
}
.test2 {
background-color: #B42626;
}
.test3 {
background-color: #19D443
}
try this
div#welcome {
height: 100vh;
background: black;
color: white;
}
div#projects {
height: 100vh;
background: yellow;
}
<div id="welcome">
your content on screen 1
</div>
<div id="projects">
your content on screen 2
</div>
it should work for you, but little support in IE
A bit of jQuery should do it:
$(document).ready(function() {
var window_height = $(window).height();
$('#section-1").height(window_height);
});
And if you want to keep 100% height on window resize:
$(document).ready(function() {
function viewport_height() {
var window_height = $(window).height();
$('#section-1").height(window_height);
}
viewport_height();
$(window).resize(function() {
viewport_height();
});
});
try this
window.onload = init;
function init()
{
var viewportHeight = "height:" + document.documentElement.clientHeight+"px;";
document.getElementById('section-1').setAttribute('style', viewportHeight);
}
Here is a script free solution, just CSS. This assumes that the divs are directly in the body element or a parent with position absolute and the parent has no padding.
#section-1 {
position: absolute;
height: 100%;
width: 100%;
background: #ff0000;
}
#section-2 {
position: absolute;
width: 100%;
top: 100%;
height: 100%;
background: #00ff00;
}
#section-3 {
position: absolute;
width: 100%;
top: 200%;
height: 100%;
background: #0000ff;
}
See fiddle: http://jsfiddle.net/QtvU5/1/

Categories