Resize div based on its content - javascript

I have this Li tag which shows notification dropdown in my bootstrap theme but the problem is if there is no content inside this li tag then also it is shown with 300px height, I have tried removing that height also but still no luck. How can I resize this li tag if there is no content inside it.
Content is shown in media-body tag
<li class="dropdown navbar-notification">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="fa fa-bell"></i>
</a>
<div class="dropdown-menu">
<div class="dropdown-header">
<span class="title">Notifications
<strong>(0)</strong>
</span>
</div>
<div class="dropdown-body niceScroll">
<div class="media-list small">
<a href="" class="media">
<div class="media-object pull-left"><i class="fa fa-ban fg-danger"></i>
</div>
<div class="media-body">
<span class="media-text">No new Notification</span>
<!-- Start meta icon -->
<span class="media-meta"></span>
<!--/ End meta icon -->
</div>
<!-- /.media-body -->
</a>
</div>
</div>
<div class="dropdown-footer">
See All
</div>
</div>
</li>
$(".navbar-notification .niceScroll").mouseover(function() {
$(".navbar-notification .niceScroll").niceScroll({
cursorwidth: '10px',
cursorborder: '0px'
}).resize();
});

You can select empty element by css :empty selector and hide it:
element:empty {
display: none;
}

Related

Bulma burger isn't shown

I have a problem with the Bulma framework. I have a small navbar for desktop and want to add a navbar burger. But it doesn't work. I tried the js script created by Bulma as an example and one from a yt video. Both didn't work. Can somebody Please help me? Down will find the html part and the js script:
js/index.js
document.addEventListener('DOMContentLoaded', () => {
// Get all "navbar-burger" elements
const $navbarBurgers = Array.prototype.slice.call(document.querySelectorAll('.navbar-burger'), 0);
// Add a click event on each of them
$navbarBurgers.forEach( el => {
el.addEventListener('click', () => {
// Get the target from the "data-target" attribute
const target = el.dataset.target;
const $target = document.getElementById(target);
// Toggle the "is-active" class on both the "navbar-burger" and the "navbar-menu"
el.classList.toggle('is-active');
$target.classList.toggle('is-active');
});
});
});
index.html
<script src="js/index.js"></script>
<nav class="navbar" role="navigation" aria-label="main navigation">
<div class="navbar-brand">
<a class="navbar-item" href="index.html">
<h1 class="title">10.8</h1>
</a>
<a role="button" class="navbar-burger" data-target="navMenu" aria-label="menu" aria-expanded="false">
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
</a>
<div class="navbar-menu" id="navMenu">
<div class="navbar-menu">
<div class="navbar-start">
<a class="navbar-item">
Home
</a>
<div class="navbar-item has-dropdown is-hoverable">
<a class="navbar-link">
More
</a>
<div class="navbar-dropdown">
<a class="navbar-item">
About
</a>
<a class="navbar-item">
Jobs
</a>
<a class="navbar-item">
Contact
</a>
<hr class="navbar-divider">
<a class="navbar-item">
Report an issue
</a>
</div>
</div>
</div>
<div class="navbar-end">
<!-- navbar items -->
</div>
</div>
</div>
</nav>
Well if watched a small video produced from Bulma, tried the code from theire website and watched another video. I also tried to write my own script but it also does'nt work.
I hope sb can help me to fix it cause this must be successfully really quick.
You have a div that doesn't close in the right place.
There is a div with duplicate className.
Properly closing and removing that div everything works as expected.
// Get all "navbar-burger" elements
const $navbarBurgers = Array.prototype.slice.call(document.querySelectorAll('.navbar-burger'), 0);
// Add a click event on each of them
$navbarBurgers.forEach(el => {
el.addEventListener('click', () => {
// Get the target from the "data-target" attribute
const target = el.dataset.target;
const $target = document.getElementById(target);
// Toggle the "is-active" class on both the "navbar-burger" and the "navbar-menu"
el.classList.toggle('is-active');
$target.classList.toggle('is-active');
});
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.9.4/css/bulma.min.css" rel="stylesheet"/>
<body>
<nav class="navbar" role="navigation" aria-label="main navigation">
<div class="navbar-brand">
<a class="navbar-item" href="index.html">
<h1 class="title">10.8</h1>
</a>
<a role="button" class="navbar-burger" data-target="navMenu" aria-label="menu" aria-expanded="false">
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
</a>
</div>
<div class="navbar-menu" id="navMenu">
<div class="navbar-start">
<a class="navbar-item">
Home
</a>
<div class="navbar-item has-dropdown is-hoverable">
<a class="navbar-link">
More
</a>
<div class="navbar-dropdown">
<a class="navbar-item">
About
</a>
<a class="navbar-item">
Jobs
</a>
<a class="navbar-item">
Contact
</a>
<hr class="navbar-divider">
<a class="navbar-item">
Report an issue
</a>
</div>
</div>
</div>
<div class="navbar-end">
<!-- navbar items -->
</div>
</div>
</nav>
</body>

Prevent going to top of page after menu button click

I have a fixed menu button that displays the menu items when is clicked. The problem is that when is clicked, it always goes to the top of the page just before the menu is open. I have tried width e.preventDefault(); and e.stopImmediatePropagation() but nothing happens. I think there should be a way to detect the scroll position to keep the menu div sticky at the same position where is open on the page. Also, note that I applied overflow:hidden to the body and HTML to keep them fixed when the menu button is clicked.
$(".nav-mobile-toggle").click(function() {
$("html,body").css("overflow", "hidden");
event.preventDefault();
});
body {
background: #fff;
font-size: 22px;
line-height: 28.6px;
color: #005153;
overflow: hidden !important;
}
html,
body {
height: 100%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="nav-mobile-toggle pt8" data-modal-id data-notification-link="nav-slide">
<div class="btn--menu">
<span class="h6 nombre">Menu</span>
<i class="icon-Align-Right icon icon--sm"></i>
</div>
</div>
<div class="notification pos-right pos-top nav-slide col-sm-4 col-xs-12 bg--primary-1" data-notification-link="nav-slide" data-animation="from-right" id="notification">
<div class="nav-slide__content">
<div class="pt104 text-left">
<ul class="menu">
<li>
<a href="/">
<span class="h3">Inicio</span>
</a>
</li>
<li>
<a href="/el-despacho.php">
<span class="h3">El despacho</span>
</a>
</li>
<li>
<a href="/quienes-somos.php">
<span class="h3">Quiénes somos</span>
</a>
</li>
<li>
<a href="/que-nos-diferencia.php">
<span class="h3">Qué nos diferencia</span>
</a>
</li>
<li>
<a href="/lo-que-opinan.php">
<span class="h3">Qué opinan de nosotros</span>
</a>
</li>
<li>
<a href="/areas-practica.php">
<span class="h3">Áreas de práctica</span>
</a>
</li>
<li>
<a href="/blog">
<span class="h3">Blog</span>
</a>
</li>
<li>
<a href="/contacto.php">
<span class="h3">Contacta con nosotros</span>
</a>
</li>
</ul>
</div>
<div class="pos-absolute pos-bottom menu-footer text-right">
<p class="tel">
123 456 789
</p>
<p class="mail">
info#example.com
</p>
</div>
</div>
</div>
add event parameter into the function:
$(".nav-mobile-toggle").click(function(event){
event.preventDefault();
});

href link button not working html

This code below for the menu, all links scroll down to a certain section, except for the Blog button that should send the user to another link, but it doesn't work, it only works if I write click and choose open in new tab
here is the code
<div class="header-menu-and-meta">
<ul id="main-menu" class="main-menu">
<li><a data-scroll-nav="4" href="#cta-title-1">contact</a></li>
<li><a data-scroll-nav="3" href="#clients-testmonials">clients</a></li>
<li><a data-scroll-nav="1" href="#fun-facts">about</a></li>
<li><a data-scroll-nav="0" href="#header">main</a></li>
</ul>
<div class="header-meta">
<div class="hm-content">
<a class="header-btn scroll-to btn small colorful hover-white" href="http://example.com/blog/">Blog</a>
</div><!-- .hm-content end -->
</div><!-- .header-meta end -->
<div class="mobile-menu-btn hamburger hamburger--slider">
<span class="hamburger-box">
<span class="hamburger-inner"></span>
</span>
</div><!-- .mobile-menu-btn -->
<!-- <div class="clearfix"></div> -->
</div><!-- .header-menu-and-meta end -->
If you want to open your link in a new tab you can use the target='_blank' attribute. If you wish to open it in the same window then don't include this attribute. See the code snippet below. (Note that the target blank attribute will not work on this site, so test it locally or on this fiddle)
Also, ensure the you have the full url, including www.
<div class="header-menu-and-meta">
<ul id="main-menu" class="main-menu">
<li><a data-scroll-nav="4" href="#cta-title-1">contact</a></li>
<li><a data-scroll-nav="3" href="#clients-testmonials">clients</a></li>
<li><a data-scroll-nav="1" href="#fun-facts">about</a></li>
<li><a data-scroll-nav="0" href="#header">main</a></li>
</ul>
<div class="header-meta">
<div class="hm-content">
<a class="header-btn scroll-to btn small colorful hover-white" href="http://www.example.com/blog/" target="_blank">Blog</a>
</div>
<!-- .hm-content end -->
</div>
<!-- .header-meta end -->
<div class="mobile-menu-btn hamburger hamburger--slider">
<span class="hamburger-box">
<span class="hamburger-inner"></span>
</span>
</div>
<!-- .mobile-menu-btn -->
<!-- <div class="clearfix"></div> -->
</div>
<!-- .header-menu-and-meta end -->
Its working fine for me it redirects to link.
<div class="header-menu-and-meta">
<ul id="main-menu" class="main-menu">
<li><a data-scroll-nav="4" href="#cta-title-1">contact</a></li>
<li><a data-scroll-nav="3" href="#clients-testmonials">clients</a></li>
<li><a data-scroll-nav="1" href="#fun-facts">about</a></li>
<li><a data-scroll-nav="0" href="#header">main</a></li>
</ul>
<div class="header-meta">
<div class="hm-content">
<a class="header-btn scroll-to btn small colorful hover-white" href="http://example.com/blog/">Blog</a>
</div><!-- .hm-content end -->
</div><!-- .header-meta end -->
<div class="mobile-menu-btn hamburger hamburger--slider">
<span class="hamburger-box">
<span class="hamburger-inner"></span>
</span>
</div><!-- .mobile-menu-btn -->
<!-- <div class="clearfix"></div> -->
</div><!-- .header-menu-and-meta end -->
You can control the behaviour of redirection of the anchor tags by setting the target attribute.
by default property it has is self: target="_self"
You want to achieve is target="_blank"
I took the image clip from Mozilla Developers Network.
Ok I found the problem it was in the JS files, the HTMLAnchorElement was set to preventdefault

How do you move a div into another using javascript?

I have two divs of the same size, and when I click a button on one of the divs, it will get larger. I want to move the smaller div into the bottom-right corner of the larger div, and when I click the button again the divs will return to their original size and position.
Before:
--- ---
| 1 | | 2 |
--- ---
After:
---------
| 2 |
| ---|
| | 1 |
---------
Here's my javascript for resizing so far:
function resizeSubscriber() {
var subscriberPanel = document.getElementById('dvOtherPerson');
var publisherPanel = document.getElementById('dvYou');
var classSetting = subscriberPanel.getAttribute('class');
if (classSetting == 'col-xl-4') {
subscriberPanel.setAttribute('class', 'col-xl-12');
}
else {
subscriberPanel.setAttribute('class', 'col-xl-4');
}
}
here is my asp code
<div id="dvOtherPerson" class="col-xl-4">
<div>
<div class="m-portlet__head">
<div class="m-portlet__head-caption">
<div class="m-portlet__head-title">
<h3 class="m-portlet__head-text">
Rachel Hawkins
</h3>
</div>
</div>
<div class="m-portlet__head-tools">
<ul class="m-portlet__nav">
<li class="m-portlet__nav-item">
<a href="" class="m-portlet__nav-link m-portlet__nav-link--icon">
<a href="" class="m-portlet__nav-link m-portlet__nav-link--icon">
<i class="fa fa-volume-up text-success"></i>
</a>
<a href="" class="m-portlet__nav-link m-portlet__nav-link--icon">
<i class="fa fa-refresh"></i>
</a>
<a onclick="resizeSubscriber();" href="#" class="m-portlet__nav-link m-portlet__nav-link--icon">
<i class="fa fa-expand"></i>
</a>
</li>
</ul>
</div>
</div>
<div class="m-portlet__body">
<img src="./img/temp/users/video_rachel.jpg" class="img-fluid" />
</div>
</div>
<%-- <div id="videos">
<div id="subscriber"></div>
</div>--%>
</div>
<div id="dvYou" class="col-xl-4">
<div class="m-portlet m-portlet--fit">
<div class="m-portlet__head">
<div class="m-portlet__head-caption">
<div class="m-portlet__head-title">
<h3 class="m-portlet__head-text">
Me
</h3>
</div>
</div>
<div class="m-portlet__head-tools">
<ul class="m-portlet__nav">
<li class="m-portlet__nav-item">
<a href="" class="m-portlet__nav-link m-portlet__nav-link--icon">
<i class="fa fa-microphone-slash text-danger"></i>
</a>
<a href="" class="m-portlet__nav-link m-portlet__nav-link--icon">
<i class="fa fa-refresh"></i>
</a>
</li>
</ul>
</div>
</div>
<div class="m-portlet__body">
<div id="publisher"></div>
</div>
</div>
</div>
Use appendChild, and it will move the source element to a target element, e.g.
target_element.appendChild(source_element)
As commented, to apply different CSS style, do like this and the source_element will automatically change when inside the target_element
source_element {...}
target_element source_element {...}
If you can use jQuery, you can use the method append():
$(<OUTER_SELECTOR>).append($(<INNER_SELECTOR>));

JS Hide header elements and resize navigation bar on scroll

I have a large header section for a website that is split into three sections; social links, logo, and then the navigation bar. I am trying to figure out how I can hide the top two elements and resize the navigation bar and have it fixed when the user scrolls past it.
The nav bar currently just holds a link that will open up a separate side menu that holds all of the actual page navigation.
https://jsfiddle.net/cxekxrxy/2/
<header>
<!-- social links -->
<div id="social" class="social-header">
<div class="container">
<div class="row">
<ul>
<li class="social-icon"><i class="fa fa-facebook-official" aria-hidden="true"></i></li>
<li class="social-icon"><i class="fa fa-twitter-square" aria-hidden="true"></i></li>
<li class="social-icon"><i class="fa fa-instagram" aria-hidden="true"></i></li>
<li class="social-icon"><i class="fa fa-linkedin-square" aria-hidden="true"></i></li>
</ul>
</div>
</div>
</div>
<!-- logo -->
<div id="logo" class="logo-header">
<div class="container">
<div class="row">
<h1 class="logo-main">Main Header</h1>
<h2 class="logo-sub">Sub Header</h2>
</div>
</div>
</div>
<!-- Navigation -->
<div id="header" class="main-header">
<div class="container">
<div id="bars"><span class="glyphicon glyphicon-menu-hamburger text-right" id="nav-bars" style="font-size:25px; cursor:pointer" onclick="openNav()"></span></div>
</div>
<!-- nav links (hidden until <span> is pressed) -->
<div id="main-nav" class="side-nav">
×
<span class="glyphicon glyphicon-home"> Home</span>
<span class="glyphicon glyphicon-user"> About</span>
<span class="glyphicon glyphicon-picture"> Portfolio</span>
<span class="glyphicon glyphicon-envelope"> Contact</span>
</div>
</div>
</header>
You can see this neat example for your need:
https://css-tricks.com/scroll-fix-content/
And work with your code.
This is combination CSS and JS, and a simplest solution
.

Categories

Resources