I have 3 different charts inside 3 different div tags that appear and hide when specified. However, two of the charts randomly resize and i know why.
How do i fix this?
I have wordpress and using visualizer plugin for my charts. have the code in the header.php file and in the text portion of the page specific code.
Code below:
Header.php snippet -
<script>
var divs = ["Daily", "Weekly", "Monthly"];
var visibleDivId = null;
function toggleVisibility(divId) {
if(visibleDivId === divId) {
visibleDivId = null;
} else {
visibleDivId = divId;
}
hideNonVisibleDivs();
}
function hideNonVisibleDivs() {
var i, divId, div;
for(i = 0; i < divs.length; i++) {
divId = divs[i];
div = document.getElementById(divId);
if(visibleDivId === divId) {
div.style.display = "block";
} else {
div.style.display = "none";
}
}
}
</script>
HTML page snippet -
<div class="inner_div">
<div id="Daily">[visualizer id="431"]</div>
<div id="Weekly" style="display: none;">[visualizer id="430"]</div>
<div id="Monthly" style="display: none;">[visualizer id="429"]</div>
</div>
<div class="main_div">
<div class="buttons">
Daily | Weekly | Monthly
</div>
A quick fix is to include call visualizer.render(); at the end of your toggleVisibility function:
function toggleVisibility(divId) {
if(visibleDivId === divId) {
visibleDivId = null;
} else {
visibleDivId = divId;
}
hideNonVisibleDivs();
visualizer.render();
}
The problem is caused by invalid calculation of available area for divs that aren't visible at the time. Calling this method forces a re-calulcation of the svgs and ensures the right size.
Related
I'm looking to scroll up a page when I have a div selected, so the goal is to drag a div and move the page up. The scoll down works strangely.
Is there a problem with angular or html that would be listed?
(I don't want to use Jquery)
<div class="panel panel-default">
<div class="panel-body">
<p class="h4">Commandes</p>
<div class="row">
<div class="col-md-12 col-xs-12 text-right" draggable="true">
<button type="button" (click)="addCommande()" class="btn btn-labeled btn-purple m-l-4">
<span class="btn-label"><i class="fa fa-plus"></i></span><span class="hidden-xs">Ajouter une commande</span>
</button>
</div>
</div>...
editadding solutions based on our discussion in the comments section
im still not sure if I got your end goal, but I hope I did
[first solution] add `scrollPositionRestoration: 'enabled'` to `app-routing.module.ts` 's `RouterModule`:
RouterModule.forRoot(routes, {scrollPositionRestoration: 'enabled'})
[second solution] try implementing this logic]
export class ScrollToTopComponent implements OnInit {
windowScrolled: boolean;
constructor(#Inject(DOCUMENT) private document: Document) {}
#HostListener("window:scroll", [])
onWindowScroll() {
if (window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop > 100) {
this.windowScrolled = true;
}
else if (this.windowScrolled && window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop < 10) {
this.windowScrolled = false;
}
}
scrollToTop() {
(function smoothscroll() {
var currentScroll = document.documentElement.scrollTop || document.body.scrollTop;
if (currentScroll > 0) {
window.requestAnimationFrame(smoothscroll);
window.scrollTo(0, currentScroll - (currentScroll / 8));
}
})();
}
ngOnInit() {}
[third solution]
If all fails, then create some empty HTML element (eg: div) at the top (or desired scroll to location) with id="top":
<div id="top"></div>
And in component:
ngAfterViewInit() {
// Hack: Scrolls to top of Page after page view initialized
let top = document.getElementById('top');
if (top !== null) {
top.scrollIntoView();
top = null;
}
}
I dont have much to work with regarding how you built your code, but I hope this might solve it (it will make draggable items scroll your screen) :)
var stop = true;
document.quertSelector(".draggable").on("drag", function (e) {
stop = true;
if (e.originalEvent.clientY < 150) {
stop = false;
scroll(-1)
}
if (e.originalEvent.clientY > ($(window).height() - 150)) {
stop = false;
scroll(1)
}
});
document.querySelector(".draggable").on("dragend", function (e) {
stop = true;
});
var scroll = function (step) {
var scrollY = window.pageYOffset;
window.pageYOffset(scrollY + step);
if (!stop) {
setTimeout(function () { scroll(step) }, 20);
}
}
I want to make a bootstrap carousel with text, on top of this 4 circles where everytime 1 circle is 'selected/hovered' the right circle and the right line underneath is shown. Something like this:
Who can help me with this issue?
Here's a vanilla JS carousel you can look at, however as others pointed out Stack Overflow is not a service to create your projects for you. You will need to research CSS more so you can get the carousel to appear how you would like.
//Changed index so 1 is actually first image, rather than starting at 0 index
var index = 1;
var paused = false;
var slideShow = [];
for (i=0; i<document.getElementsByClassName("slideShow").length; i++) {
slideShow[i] = document.getElementsByClassName("slideShow")[i];
slideShow[i].style.display = "none";
}
slideShow[0].style.display = "inline";
var slides = setInterval(function() {
if (index < slideShow.length) {
index++;
showDivs();
}
else {
index = 1;
showDivs();
}
},1000);
function control(n) {
clearInterval(slides);
if (index+n > slideShow.length) {
index = 1;
}
else if (index+n <= 0) {
index = slideShow.length;
}
else {
index += n;
}
showDivs();
}
function showDivs() {
//Hide all slideShow elements, and then show only the targeted element
for (let i=1; i<=slideShow.length; i++) {
slideShow[i-1].style.display = "none";
}
slideShow[index-1].style.display = "inline";
}
<button onclick="control(-1)" class="arrows" id="left"><</button>
<p class="slideShow">1</p>
<p class="slideShow">2</p>
<p class="slideShow">3</p>
<p class="slideShow">4</p>
<p class="slideShow">5</p>
<button onclick="control(1)" class="arrows" id="right">></button>
So I have it so far when you click on an image it shows a Div. I just need it so that when you click on the image again and the div is being shown then it will hide.
Here is my code:
Style and script
<style type="text/css">
.show{display:block;}
.hide{display:none;}
</style>
<script type="text/javascript">
function showImg()
{
var obj=document.getElementById('calcShow');
obj.className = 'show';
}
</script>
Here is the HTML
<li data-value="iconchange"><img src="modules/icons/icons/calculator.png" width="65" onclick = "showImg()" class="calculator"></li>
And the Div
<div id="calcShow" class="hide"><br><br><br>
<?php
include("modules/calc/calc.html");
?>
</div>
What you want to do is create a conditional that checks whether the <div> is currently shown or not. Depending on the visibility, change the class that's associated to the <div>:
function toggleImg() {
var obj = document.getElementById('calcShow');
if (obj.className == 'show') {
obj.className = 'hide';
}
else if (obj.className == 'hide') {
obj.className = 'show';
}
}
.show {
display: block;
}
.hide {
display: none;
}
<div id="calcShow" class="hide">Hidden</div>
<br />
<img src="http://placehold.it/100" onclick="toggleImg()">
Note that you don't actually have to use classes for this, as this can be done directly through the .style.display property:
function toggleImg() {
var obj = document.getElementById('calcShow');
if (obj.style.display == 'none') {
obj.style.display = 'block';
}
else if (obj.style.display == 'block') {
obj.style.display = 'none';
}
}
<div id="calcShow" style="display: block">Hidden</div>
<br />
<img src="http://placehold.it/100" onclick="toggleImg()">
Hope this helps! :)
Solution with Jquery:
HTML:
<li data-value="iconchange"><img src="modules/icons/icons/calculator.png" width="65" class="calculator"></li>
The DIV:
<div id="calcShow"><br><br><br>
<?php
include("modules/calc/calc.html");
?>
</div>
The Function:
$('.calculator').click(function{
var isvisible = $('#calcShow').is(":visible");
if(isvisible == true){
$('#calcShow').hide();
}
else{
$('#calcShow').show();
}
})
This is basically one of the easiest way using pure javascript,
function showImage(){
var obj = document.getElementById('calcShow');
if(obj.style.display !== "block"){
obj.style.display = "block";
}else{
obj.style.display = "none";
}
}
Instead of creating separate functions for showing and hiding the image, you could create a function that toggles the value. You can use the ternary operator to decide whether you want to set the className to show or hide.
The ternary operator works like so, condition ? ifTrue : ifFalse.
The condition will be obj.className === "show". If this is true, then it should switch from its current value of "show" to a value of "hide". However, if it's false, then it's current value is "hide" and so its new value should be set to "true".
function showOrHide() {
var obj = document.getElementById('calcShow');
obj.className = (obj.className === "show" ? "hide" : "show");
}
Then you can simply set the onClick attribute to call this function.
Using this HTML code, when I click the hide link, it hides the block but also adds a line feed. How to eliminate the new/additional line feed?
Thank you.
function showTop1(){
document.getElementById("top1opener").style.display = "none";
document.getElementById("top1").style.display = "block";
}
function hideTop1(){
document.getElementById("top1").style.display = "none";
document.getElementById("top1opener").style.display = "block";
}
"line feed" you mind that's element is block and stretching to 100% width?
<div id="top1opener" style="background-color:red;" onclick="showTop1()">
OPENER CONTENT
</div>
<div id="top1" style="background-color:green" onclick="hideTop1()">
TOP1 CONTENT
</div>
<script>
function showTop1() {
document.getElementById("top1opener").style.display = "none";
document.getElementById("top1").style.display = "inline";
}
function hideTop1() {
document.getElementById("top1").style.display = "none";
document.getElementById("top1opener").style.display = "inline";
}
</script>
is that you means? elements in this code doesnt stretching to 100% width.
I have the following code which works to create the div on click, but I would like the div to slide down slowly and not appear immediately:
function hideshow(which) {
if (!document.getElementById) return
if (which.style.display == "none") which.style.display = "block"
else which.style.display = "none"
I've found some code that appears to do what I'm after, but I'm having difficulty integrating it into my existing code (above):
$(document).ready(function() {
$("#button").toggle(function() {
$(this).text('Hide');
}, function() {
$(this).text('show');
}).click(function(){
$("#hidden_content").slideToggle("slow");
});
I've stuck it on js fiddle if that helps (with supporting css):
http://jsfiddle.net/dRpWv/479/ and
http://jsfiddle.net/dRpWv/447/
Use jquery property "slideDown"
My Codepen
function hideshow() {
var i = 1;
if (document.getElementById('effet').style.display == "none") {
$('#effet').slideDown("normal");
document.getElementById('effet').style.display = "block";
i = 2;
} else {
if (i == 1) {
$('#effet').slideUp("normal", function () {
document.getElementById('effet').style.display = "none";
});
}
}
}