Place background image on the right of the dropdown menu - javascript

I have a dropdown menu.
I want to move my icons to the far right. I've tried
if (item.logo) {
result.find("a")
.addClass("ui-menu-item-icon")
.css("background-image", "url(" + item.logo + ")")
.css('background-repeat', 'no-repeat')
.css('background-position', 'top right')
.css('background-size', '28px 19px');
}
I couldn't seem to make it work.
Here is my
JSfiddle
Any hints ?

Add this to your CSS
.ui-menu-item-icon {
display: block;
}

Two things you can try
Add padding at right side so your image moves a little further.
Make your link display: block; not sure if it will work. Wont if image is not in flow.

Add this to your CSS, to make the icons appear right after the text.
a.ui-menu-item-icon {
padding-right: 30px;
}
https://jsfiddle.net/4sv3cpeu/2/
Other option:
Add this to make the Icons appear on the far right end of the option
a.ui-menu-item-icon {
display: block;
}
https://jsfiddle.net/4sv3cpeu/4/

If your a elements are in a list, try adding a width to both of them. In this case I assume your dropdown element (list item's parent) has a fixed width, so adding a width: 100%; should do the trick. For example:
li {
width: 100%;
}
li a {
display: block;
width: 100;
}
The a element needs to be displayed as a block to be given a size.

Add this to your css
li a {
display: block;
}
Demo : https://jsfiddle.net/s75o5xo6/

Related

how to relocate brand image on the navbar?

I am trying to make a navbar where brand image can be relocated when scrolling down like the example shown on http://www.agilent.com/home
I know how to addclass using jquery, but I don't know how to add an image when scrolling down or is there any other method to make this effect happen.
How can I achieve this effect? Thanks in advance!
You need to use two images. One on the heading and one on the nav.
This snippet will detect if the navbar is on top and add sticky class to a nav element.
$(window).scroll(function() {
if ($(window).scrollTop() > $("nav").offset().top) {
$("nav").addClass("sticky");
} else {
$("nav").removeClass("sticky");
}
});
When the nav element uses sticky class, it should display the nav logo.
nav .logo {
display: none;
}
nav.sticky {
position: fixed;
top: 0;
}
nav.sticky .logo {
display: inline;
}
jsfiddle demo

expanding an inline-block jumps to above row

I have a series of articles
<section>
<article>1</article>
<article>2</article>
<article>3</article>
....
<article>999</article>
<section>
arranged in a grid of three per row
section {
position: relative
}
article {
display: inline-block;
width: 33%;
}
and I want that they expand horizontally to fully occupy its row when clicked.
so I create an expanded class
.expanded {
position:absolute;
left:0px;
width: 100%;
}
and apply it on click
$("article").click(function(){
$(this).toggleClass("expanded");
});
I am almost there, as you can see in the fiddle here http://jsfiddle.net/aqw339r0/1/
When I click on any of the articles they expand to the full width of the row; but the problem is that when I click the first article of a row (except the first row) it "jumps" to the row immediately above.
I have tried changing the position and display attributes of both article and .expanded but I cannot get the behavior I need. For example: When I change the position:absolute of .expanded, the blocks 5 and 6, "jump" to the row below. Similar when I remove display:inline-block and change to float:left.
Do you have any ideas of why this happens and how to correct it?
Using the solution from #Rick (minus the css top edit) and then toggling an additional element to keep other elements in place, this should do what is needed: Fiddle
$("article").click(function() {
$(this).toggleClass("expanded");
if ($(this).hasClass("expanded"))
{
$(this).before("<article class='blank'> </article>");
}
else
{
$(this).prevAll('.blank').first().remove();
}
});
To explain further, when the element is clicked:
if ADDING the class 'expanded', then this will also add a blank article element (before) as a placeholder to keep the flow consistent. This is important because when the element with class 'expanded' becomes position: absolute it is removed from the normal flow of the content.
if REMOVING the class 'expanded', then this will also remove the blank article placeholder element. The prevAll function looks through all previous elements with the class 'blank' and then takes the first() one and removes it. Because it was inserted 'before', this is guaranteed to remove the correct blank article placeholder.
When position:absolute is set on an <article> element, that takes it out of the normal content flow, and causes the offset. We can correct it by adding an empty <article> as a placeholder when the click triggers, and removing it when click again.
var open = false;
jQuery("article").click(function () {
open = !open;
if (open) {
$(this).addClass("expanded");
$(this).before("<article class='holder'> </article>");
} else {
$("article").removeClass("expanded");
$(".holder").remove();
}
});
p.s. I'm not that good at jQuery, any suggestions are welcome.
jsfiddle
I can't think of a pure CSS solution, but since you're using jQuery anyway, simply hard-code the expanded element's top:
$("article").click(function() {
$(this).css('top', $(this).position().top);
$(this).toggleClass("expanded");
});
Fiddle
The top style will be ignored once the expanded class is removed.
Try this
<section>
<article>1</article>
<article>2</article>
<article>3</article>
</section>
<section>
<article>4</article>
<article>5</article>
<article>6</article>
<section>
<section>
<article>7</article>
<article>8</article>
<article>9</article>
</section>
section {
position:relative;
}
article {
width: 30%;
min-width: 200px;
min-height: 200px;
display: inline-block;
border: 1px solid black;
}
.expanded {
width: 100%;
background-color:#eef;
left:0px;
top:0;
position:absolute;
transition: 0.2s linear;
}
// if you want others to close while expanding
$("article").click(function(){
$('article').removeClass("expanded");
$(this).toggleClass("expanded");
});
// esle if you want everything to be expanded
$("article").click(function(){
$(this).toggleClass("expanded");
});

Hide portion of element offscreen and push & reveal on click

I'm building a responsive page that, when viewed on mobile, includes some popular behaviors.
I have an element that includes text and a link. The text portion needs to cover 100% of the width of the viewport and when clicked/tapped the link should push the text content left and reveal the link.
Fiddle: http://jsfiddle.net/Flignats/0n0fwm20/
HTML:
<div id="container">
<div id="block-one">
I'm a bunch of informational text.
</div>
<div id="block-two">
I'm a link!
</div>
</div>
CSS:
#container {
width: 100%;
height: 100px;
}
#block-one {
float: left;
width: 75%;
height: 100px;
background-color: #626366;
}
#block-two {
float: left;
width: 25%;
height: 100px;
background-color: #969696;
}
#block-two a {
color: blue;
}
How can I have block-one span the full width of the screen, block-two hidden off the screen, and, on click, animate (the margin?) block-two into view with block-one pushed to the left?
My apologies if I missed a clear example on site, most other questions were more complex.
I'd prefer not to build a jquery mobile page, though the push and reveal panels would accomplish this action. I'm also using Angular if nganimate is preferred over javascript.
Here is pure css solution: http://jsfiddle.net/3wcfggmf/
I've used trick with label and checkbox:checked to recognize if element is clicked or not.
input:checked + #block-one {
width: 75%;
}
If I didn't understand you correctly please let me know and I'll modify this PURE css solution for you :)
I forked your fiddle here with a working example: http://jsfiddle.net/90gm224q/
Added CSS to yours:
#container * { transition: width .4s; }
#container.clicked #block-one { width:25%; }
#container.clicked #block-two { width:75%; }
The JS:
var container = document.getElementById('container'),
link = document.getElementById('block-two').getElementsByTagName('a')[0];
link.onclick = function() {
container.className = 'clicked';
return false;
}
Assuming I understood your question correctly, you can play with the CSS values for width to achieve your desired effect.
You could accomplish this via CSS transistions and a JavaScript class toggle.
Demo - http://jsfiddle.net/Leh5t9t6/
In my demo, clicking #block-one toggles a class which fires the CSS transision. I included a pure JavaScript and jQuery version in the demo (you mentioned you'd prefer not use jQuery).
I also edited the styles slightly to accommodate the off-screen link.
Pure Javascript
var element = document.getElementById('block-one');
element.addEventListener('click', function () {
this.classList.toggle('reveal');
}, false);
jQuery
$('#block-one').on('click', function(){
$(this).toggleClass('reveal');
});

How can I .slidetoggle a div the .length of a string in another div

I have a label where if you hover over it, it should expand the length of the text inside. I would like to slide the div next to it the width of the label (depending on the length of the word). Here is the JS Fiddle: http://jsfiddle.net/5s69j/1/
I want to use the .length to slide the div next to it because I don't want to hardcode it.
I ultimately want to have the title div visible at all times if there is a label or not. If there IS a label then I want the title to be to the right of the label at all times. If there ISN'T a label then I don't want space before the title, which is why I'm not using margin.
Here I'm trying to set a hover function on the labels to slide the title the length of label. And I think that I need something to calculate the length of a string. Let me know if I'm missing something obvious.
$(function(){
var stuff = $(".labels").length;
$('.labels').hover(function{
$(".title").slideToggle(stuff, function() {
});
});
});
Thank you so much!
Check this, here is solution without js
HTML
<div class="labels">
<div class="new"></div>
<div class="firstlabel">label label</div>
</div>
<div class="title">title goes here</div>`
* {
transition: all .5s ease;
}
.labels {
float: left;
overflow: hidden;
max-width: 20px;
color: #7cbf50;
background-color: #7cbf50;
}
.firstlabel {
white-space: nowrap;+
}
.labels:hover {
max-width: 500px;
}
.labels:hover .firstlabel {
color: #fff;
}
or see the following
http://jsfiddle.net/5s69j/8
JSFiddle - let me know if you want anything changed. You just needed to have them display: inline-block; and remove the absolute positioning.
DEMO Check this.
display:inline-block
Use the above. It ll solve your problem

Strange css position issue

I'm using Choosen and Twitter Bootstrap in my project. What I want to get is, to get choosen's dropdown over collapsible divs but it goes under other content. Here is jsfiddle
http://jsfiddle.net/tt13/CFbpt/5/
What am I missing? how to fix this problem?
This should take care of everything:
.collapse.in { overflow: visible; }
.chzn-drop { display: none; }
.chzn-container-active .chzn-drop { display: block; }
http://jsfiddle.net/Wexcode/7HLyZ/3/

Categories

Resources