I'm migrating to bootstrap 3.0.0 and I'm having issues with an affixed menu to the left: as soon as it becomes affixed (after 10px scroll), its width changes. In this fiddle it gets smaller, in my real site it gets wider and expands on the actual content.
It worked perfectly with bootstrap v2.3.2. After checking it looks like the list items don't play well with the .affix {position: fixed;} that appears.
Any ideas?
SOLUTION: based on the latest comments I have finally added this JS piece which fixes it nicely without having to set a fixed width to the affixed element:
$(function() {
var $affixElement = $('div[data-spy="affix"]');
$affixElement.width($affixElement.parent().width());
});
I had the same problem and fixed with this:
$(window).resize(function () {
$('#category-nav.affix').width($('#content').width());
});
basically in an event of resize I calculate the content div's width and set the width of affixed element to that.
$(window).scroll(function () {
$('#secondary .widget-area.affix').width($('#secondary').width());
});
Here's another version:
$('.sitebar .affix-top').width($('.sitebar .affix-top').width());
$(window).resize(function () {
$('.sitebar .affix').width($('.sitebar .affix-top').width());
});
$(window).scroll(function () {
$('.sitebar .affix').width($('.sitebar .affix-top').width());
});
Here's my version.
var fixAffixWidth = function() {
$('[data-spy="affix"]').each(function() {
$(this).width( $(this).parent().width() );
});
}
fixAffixWidth();
$(window).resize(fixAffixWidth);
CSS
div.affix {
position: fixed !important;
}
had the same problem once (only in BS 2.3.2).
Not an answer, more a hack: I gave the affixed element a width. That sucked, because i had to set width for all resolutions (RWD) and actually the value should be standard column width (e.g. .span4).
Yes: position: fixed takes the element out of the given context (in your case col-md-3).
I use this code to fix the width of the affix.
$(document).on('affixed.bs.affix',function(e){
$('.affix').each(function(){
var elem = $(this);
var parentPanel = $(elem).parent();
var resizeFn = function () {
var parentAffixWidth = $(parentPanel).width();
elem.width(parentAffixWidth);
};
resizeFn();
//$(window).resize(resizeFn);
});
});
The affix get the width of his parent and check that width on every scroll the web. Uncommenting the last line, execute too on resize window event.
I used $('.affix-element').width($('.affix-element-parent').width()).affix()
Works well :)
My solution as well:
$('.menu-card').affix();
$(document).on('affix.bs.affix', '.menu-card', function() {
$(this).width($(this).width());
});
(.menu-card is my sticky div)
I added this for supporting the window resizing:
Let's assume the affix are in a parent div #menu-card-pane.
$(window).resize(function () {
var parentSize = $('#menu-card-pane').width();
$('.affix').each(function() {
var affixPadding = $(this).innerWidth() - $(this).width();
$(this).width(parentSize - affixPadding);
});
});
Here is my HTML
<div class="sidebar-nav">
<div class="navbar navbar-default" role="navigation">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#committees-navbar">
<span class="sr-only"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<span class="visible-xs navbar-brand">Committees</span>
</div>
<div id="committees-navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav nav-stacked" data-spy="affix" data-offset-top="250">
<li class="col-xs-12"><a ng-click="scrollTo('boardCommittee')">BOARD OF DIRECTORS</a></li>
<li class="col-xs-12"><a ng-click="scrollTo('madrasaCommittee')">MADRASATUL JAMALIYAH</a></li>
<li class="col-xs-12"><a ng-click="scrollTo('shababCommittee')">SHABAB</a></li>
<li class="col-xs-12"><a ng-click="scrollTo('bwaCommittee')">BURHANI WOMEN</a></li>
<li class="col-xs-12"><a ng-click="scrollTo('tncCommittee')">TAISEER UN NIKAH</a></li>
<li class="col-xs-12"><a ng-click="scrollTo('fmbCommittee')">FAIZUL MAWAIDUL BURHANIYAH</a></li>
<li class="col-xs-12"><a ng-click="scrollTo('websiteCommittee')">WEBSITE</a></li>
<li class="col-xs-12"><a ng-click="scrollTo('tadfeenCommittee')">TADFEEN</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
And this is my JS fix
$(function () {
var resize = function () {
var sidebarNav = $(".sidebar-nav");
var sidebarNavAffix = $(".sidebar-nav .affix");
if (sidebarNavAffix.length && (sidebarNavAffix.width() !== sidebarNav.width())) {
sidebarNavAffix.width(sidebarNav.width());
}
}
$(window).on({"scroll" : resize, "resize" : resize});
});
Related
I´m using bootstrap3 and I´m trying to create a fixed menu for mobile devices. This menu can have submenu´s and submenu´s can have a lot of content in it. So when you watch the site on mobile phone´s (320x480 eg) and the menu is very long then you can´t scroll in the inner/submenu. In other words if your screen height is too short you can´t see all the menu items because the menu bar is sticked at the top of the screen.
I´ve read a lot of post from github and here + Bootstrap's page, but can´t seem to get it to work. I think that position fixed is the problem?!
I'll try to explain with a fiddle and here below
EDIT: You can view an example here -> example (resize browser and try to hit brands)
HTML
<div id="main-nav-container">
<div class="container">
<div class="row">
<div class="col-md-12 clearfix">
<nav id="main-nav">
<div id="responsive-nav">
<div id="responsive-nav-button">
Menu <span id="responsive-nav-button-icon"></span>
</div><!-- responsive-nav-button -->
<ul class="clearfix responsive-nav">
<li><i class="fa fa-home"></i></li>
<li><span class="menu-button"></span><a title="Camera" href="http://www.webshop.com/nl/camera/">Camera</a>
<ul>
<li><a title="Canon" href="http://www.webshop.com/nl/camera/canon/">Canon</a></li>
<li> like 40 li items more for example </li>
</ul>
</li>
etc...
JQUERY
$(function() {
var sticky_navigation_offset_top = $('#main-nav-container').offset().top;
// our function that decides weather the navigation bar should have "fixed" css position or not.
var sticky_navigation = function(){
var scroll_top = $(window).scrollTop(); // our current vertical position from the top
// if we've scrolled more than the navigation, change its position to fixed to stick to top, otherwise change it back to relative
if (scroll_top > sticky_navigation_offset_top) {
$('#main-nav-container').css({ 'position': 'fixed', 'top':0, 'left':0 });
} else {
$('#main-nav-container').css({ 'position': 'relative' });
}
};
sticky_navigation();
// and run it again every time you scroll
$(window).scroll(function() {
sticky_navigation();
});
});
So how can I make the menu so that the with the li items is scrollable so that on smaller screens the whole submenu can be viewed?
I'm developing a website using bootstrap and its responsive JS+CSS.
At the top of the page I have a fixed navigation bar where an "expand menu" button is shown in case the viewport is too tight. This button does its magic with an animation (a CSS3 one I think) and I'm happy with it, but I would like to do something more (toggle classes with jquery) each time the animation finishes (both the open animation and the close one).
I was thinking about a javascript listener (even better by defining it thanks to jquery .on function), but I really don't know what event I should listen to!
Any ideas?
UPDATE
I've fond out that by listening to this event on the object I wanna control almost does the job well:
$("#main-navbar .nav-collapse").on("transitionend", function(event){
console.log("end of the animation");
}
the only problem is that it messes bootstrap animations up on that object: the first time it works, but wen I want to close the expanded navbar, nothing happens (it seems that my listener overrides the bootstrap ones. quite weird, huh?)
If you use bootstrap and transitions (css3 transition) you can try this:
$("body").on($.support.transition.end, '#main-navbar .nav-collapse', function(event){
console.log("end of the animation");
});
$.support.transition.end contains one of these events: webkitTransitionEnd, transitionend, oTransitionEnd otransitionend, transitionend.
But if you use css3 animation (css3 animation-name and keyframes) you can try this:
$("body").on('webkitAnimationEnd oanimationend msAnimationEnd animationend', '#main-navbar .nav-collapse', function(event){
console.log("end of the animation");
});
$target.on("shown.bs.collapse", function(event){
console.log("end of the animation");
});
Wonder that nobody mention about Util.emulateTransitionEnd() which was created on purpose and used by Bootstrap components to catch when the animation is over.
$('#nav')
.one(
'bsTransitionEnd', // Util.TRANSITION_END
handler
)
.emulateTransitionEnd(600); // Collapse.TRANSITION_DURATION
In your particular case, you might want to extend corresponding Bootstrap plugin, so that you can figure out which exactly case is yours - Collapse is used by different components.
Here is jsfiddle snippet to demo (based on Bootstrap v4).
(function($) {
var Collapse = $.fn.collapse.Constructor;
var navbar = $('#nav');
$.extend(Collapse.Default, {
navbarClass: ''
});
var _show = Collapse.prototype.show;
Collapse.prototype.show = function() {
_show.apply(this, Array.prototype.slice.apply(arguments));
var navbarClass = this._config.navbarClass;
if (navbarClass && !navbar.hasClass(navbarClass)) {
navbar.addClass(navbarClass);
}
}
var _hide = Collapse.prototype.hide;
Collapse.prototype.hide = function() {
_hide.apply(this, Array.prototype.slice.apply(arguments));
var navbarClass = this._config.navbarClass;
if (navbarClass && navbar.hasClass(navbarClass)) {
navbar
.one('bsTransitionEnd', function() { // Util.TRANSITION_END
navbar.removeClass(navbarClass);
})
.emulateTransitionEnd(300); // Collapse.TRANSITION_DURATION / 2
}
}
})(window.jQuery);
<nav id="nav" role="navigation" class="navbar fixed-top navbar-light bg-light">
<button type="button" class="navbar-toggler" data-toggle="collapse" data-target="#nav_items" data-navbar-class="navbar-dark bg-dark" aria-expanded="false" aria-label="Menu">
<span class="text-hide">Menu</span>
<span class="navbar-toggler-icon"></span>
</button>
<div id="nav_items" class="collapse navbar-collapse">
<div class="nav navbar-nav">
<a class="nav-item nav-link active" href="">Link <span class="sr-only">Home</span></a>
<a class="nav-item nav-link" href="">Link2</a>
<a class="nav-item nav-link" href="">Link3</a>
</div>
</div>
</nav>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">
<style type="text/css">
button:focus {
outline-width: 0;
}
.navbar-collapse.collapse.show {
height: 100vh;
}
.navbar-nav {
height: 100vh;
}
.navbar-toggler {
border: none;
padding: 0.25rem 0;
}
.navbar-dark .navbar-toggler-icon {
background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='rgba(255,255,255,.5)' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cline x1='18' y1='6' x2='6' y2='18'%3E%3C/line%3E%3Cline x1='6' y1='6' x2='18' y2='18'%3E%3C/line%3E%3C/svg%3E");
}
</style>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.min.js" integrity="sha384-a5N7Y/aK3qNeh15eJKGWxsqtnX/wWdSZSKp+81YjTmS15nvnvxKHuzaWwXHDli+4" crossorigin="anonymous"></script>
jQuery has an animation listener built in: http://docs.jquery.com/Selectors/animated
Example: if( $(foo).is(':animated') ) {...}
Then if an element is !animated you can can apply whatever logic you want.
$('.collapse').on('bsTransitionEnd', function(e) {
console.log('finished')
})
This worked for me for collapsible panels of any kind. You might try with a different selector.
This question already has answers here:
How to affect other elements when one element is hovered
(9 answers)
Is there any way to hover over one element and affect a different element? [duplicate]
(2 answers)
Closed 8 years ago.
Please see the page here:
http://176.32.230.17/printingcrazy.com/branding-services
I'm trying to achieve a hover effect, so if you hover over any of the services listed, the relevant image will have a border for example.
And if you hover over the Image, the relevant text will change color.
Elements on the left are in a separate parent to the ones on the right.
Yes I have seen CSS: Hover one element, effect for multiple elements? and have tried everything, but cannot get it to apply to my circumstances.
Any help would really be appreciated!
<div class="servicepage">
<div class="serviceleft">
<img src="/digitalprint.jpg">
<img src="/dyesub.jpg">
</div>
<div class="serviceright">
<ul>
<li>
<h3>Digital Print</h3>
</li>
<li>
<h3>Dye Sublimation</h3>
</li>
</ul>
</div>
</div>
here is a simplified example. hope it helps you get started.
basically we are setting callback functions when the mouse is hovered and left on the div foo. And in those functions we are changing the css properties of the img, in this case, adding a border and removing it.
http://jsfiddle.net/btevfik/YC2tg/
JQUERY
$(document).ready(function () {
$(".foo").hover(function () {
$(".bar").css("border", "5px red solid");
});
$(".foo").mouseleave(function () {
$(".bar").css("border", "none");
});
});
HTML
<div class="foo">HOVER HERE</div>
<img class="bar" src="http://placekitten.com/100/100" />
You could do this.
HTML:
<div class="servicepage">
<div class="serviceleft">
<img class="digitalprint" src="/digitalprint.jpg">
<img class="dyesub" src="/dyesub.jpg">
</div>
<div class="serviceright">
<ul>
<li class="digitalprint">
<h3>Digital Print</h3>
</li>
<li class="dyesub">
<h3>Dye Sublimation</h3>
</li>
</ul>
</div>
</div>
Jquery:
$(".serviceleft > img").hover(
function () {
var imgclass = $(this).attr("class");
$("li." + imgclass).css("color", "red");
},
function () {
var imgclass = $(this).attr("class");
$("li." + imgclass).css("color", "black");
});
I've tried to reproduce code for your markup on the site, and this jQuery code should work, just copy/paste it on your site.
<script type="text/javascript">
$(document).ready(function(){
$(".serviceright h3").mouseenter(function(){
var indexH3 = $(this).parent().index();
$(".serviceleft .spotlight").eq(indexH3).addClass("border");
});
$(".serviceright h3").mouseleave(function(){
$(".serviceleft .spotlight").removeClass("border");
});
$(".serviceleft .spotlight").mouseenter(function(){
var indexA = $(this).index();
$(".serviceright h3").eq(indexA).addClass("redtext");
});
$(".serviceleft .spotlight").mouseleave(function(){
$(".serviceright h3").removeClass("redtext");
});
});
</script>
You must add some CSS classes for hover effect, for example
.border {
border: 1px solid red;
}
.redtext {
color: red;
}
Set properties for hover effect, and if you need to rename those classes, change those names in jQuery code also.
And here is DEMO
I am developing a task in using phonegap and jquerymobile.
How should i do to change the color of a clickable item when i click on them?
I have written the code below:
$(".testbutton").click(function(){
$(".testbutton").css("background-color","yellow");
});
But the background color will only change yellow after i release my mouse on it. I wan it to change to yellow whenever my mouse is clicked on tat item, and recover to its original background color when i release the mouse.
Can anyone please help?
Is this what you're trying to accomplish?
Live Example:
http://jsfiddle.net/A2bd3/16/
CSS:
.task-bg-color {
background-color: yellow;
}
JS:
$('#clickAndHold').mousedown(function() {
$(this).children().addClass('task-bg-color');
$('ul#tasks').listview('refresh');
});
$('#clickAndHold').mouseup(function() {
$(this).children().removeClass('task-bg-color');
$('ul#tasks').listview('refresh');
});
$('#clickAndStick').click(function() {
$(this).children().addClass('task-bg-color');
$('ul#tasks').listview('refresh');
});
$('#clickAndToggle').click(function() {
if ($(this).children().hasClass('task-bg-color')) {
$(this).children().removeClass('task-bg-color');
} else {
$(this).children().addClass('task-bg-color');
}
$('ul#tasks').listview('refresh');
});
HTML:
<div data-role="page" class="type-home">
<div data-role="content">
<ul data-role="listview" data-inset="true" data-theme="c" data-dividertheme="f" id="tasks">
<li data-role="list-divider">Tasks (Background actions)</li>
<li id="clickAndHold">Click & Hold</li>
<li id="clickAndStick">Click & Stick</li>
<li id="clickAndToggle">Click & Toggle</li>
</ul>
</div>
</div>
I have a dropdown menu:
<div class="buttons">
<div class="dropdown">
<span class="label">File</span><span class="toggle"></span>
<div class="dropdown-slider">
<span class="label">New</span>
<span class="label">Save</span>
</div> <!-- /.dropdown-slider -->
</div> <!-- /.dropdown -->
</div>
And here is js code:
<script>
$(document).ready(function() {
// Toggle the dropdown menu's
$(".dropdown .button, .dropdown button").click(function () {
$(this).parent().find('.dropdown-slider').slideToggle('fast');
$(this).find('span.toggle').toggleClass('active');
return false;
});
});
// Close open dropdown slider by clicking elsewhwere on page
$(document).bind('click', function (e) {
if (e.target.id != $('.dropdown').attr('class')) {
$('.dropdown-slider').slideUp();
$('span.toggle').removeClass('active');
}
});
</script>
If I put the two menus (one above other) I'll get popup menu below the second menu line.
How can I fix it?
Adding z-index:999; to div.dropdown-slider should fix your problem pretty quickly.