jQueryUI effects not working properly on sortable interactions - jquery-ui

Here is my Html file
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<link rel="stylesheet" href="effects.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script src="effects.js"></script>
<main>
<h2>Rank our Customer Support products</h2>
<h3>Drag up or down to rank</h3>
<ul id="vsupport">
<li class="ui-state-default">Blog / How-To Articles</li>
<li class="ui-state-default">Discussion Forum</li>
<li class="ui-state-default">Knowledge Base</li>
<li class="ui-state-default">Phone Support</li>
<li class="ui-state-default">Wiki Support</li>
</ul>
<br>
<input type="submit" id="submitbutton" value="Submit ">
</main>
and the effects.js file
$(document).ready(function(){
$("#vsupport").sortable({
placeholder:"ui-state-highlight",
start:function(event,ui){
$(ui.item).effect("pulsate",{times:3},1500);
},
update:function(event,ui){
$(ui.item).effect("highlight",{color:"#7fffd4"},2000);
}
});
$("#submitbutton").button().on("click",function(){
$("#vsupport li").removeAttr("style");
});
});
My question is, when I drag the li element to sort it, the li element disappears, and through debug tool,Isaw some css style was added to the li element:
<li class="ui-state-default ui-sortable-handle" style="width: 313px; height: 18px; position: absolute; z-index: 1000;">Knowledge Base</li>
and if I remove this css style, the li element appears again.
Anyone knows why this happens and any solutions to it?
Any answer is appreciated, thanks!

It is because of how 'pulsate' operates. It will set the position of the dragged item as 'absolute' while pulsating. Thus, after the item is dropped, the next item will disappear underneath the dragged item. If you finish holding the item until pulsating is finished, then everything will operate as intended.
Therefore, let's fix the position after pulsating stops:
$(document).ready(function(){
$("#vsupport").sortable({
placeholder:"ui-state-highlight",
start:function(event,ui){
$(ui.item).effect("pulsate",{times:3},1500,function() {
$(this).css('position','initial'); //fixing the position
})
},
update:function(event,ui){
$(ui.item).effect("highlight",{color:"#7fffd4"},2000);
},
stop:function(event,ui){
}
});
$("#submitbutton").button().on("click",function(){
$("#vsupport li").removeAttr("style");
});
});

Related

jQuery UI select outer container and not inner container for drop target

I am trying to select only the outer container for the drop target with jQuery UI.
I am trying to drop in media-content but not in media-list-items.
The reason is that media-list-items is not the full width of it's parent and I only want to drop to the right side which is a different background color the full height.
HTML:
<div class="media-content clearfix ui-droppable on" style="">
<form method="post" class="form-horizontal" action="./">
<fieldset>
<div class="media-list-items clearfix">
<ul data-gallery_id="18" class="media-list ui-sortable" style="">
<li id="media_17"></li>
<li id="media_15"></li>
</ul>
</div>
</fieldset>
</form>
</div>
JS:
var $trash = $('.media-content:not(.media-list-items), .media-content');
$trash.droppable({
accept: '.media-list > li',
drop: function(event, ui) {
console.log('trashed');
}
});
I'm not sure you can achieve what you want without adding another element as your "trash" droppable. With some neat css you can make it look pretty much the same :)
html
<div class="media-content">
<form method="post" class="form-horizontal" action="./">
<fieldset>
<div class="trash"></div> <!-- added a trash element -->
<div class="media-list-items">
<ul class="media-list">
<li id="media_25"></li>
<li id="media_26"></li>
<li id="media_27"></li>
</ul>
</div>
</fieldset>
</form>
</div>
css
.media-content {
position: relative; /* makes it contain absolutely positioned descendants */
}
.media-content .trash {
position: absolute;
top: 0; bottom: 0; /* stretches to full height of .media-content */
right: 0;
width: 100px;
background:#f5f5f5;
}
js
$('.media-content .trash').droppable({
drop: function(event, ui) {
console.log('trashed');
}
});
Demo at: http://jsfiddle.net/KazeG/6/

jQuery Datepicker Width

I don't understand why this is so hard to do. Everything about jQuery is so simple. How do you set the width of a jQuery "display: inline;" Datepicker?
I have edited the jquery ui css but as soon as I change the month it resets the width.
I hope this is just something dumb that I'm missing.
If you are using the jQuery datepicker in its default form, then just add this to your css and it should work:
.ui-datepicker {
width: 17em; /*what ever width you want*/
}
Reducing the font size of the calendar will reduce the width of the date picker.
You can reduce the font size by adding this css:
div.ui-datepicker
{
font-size:10px;
}
I solved this using a wrapper, and setting a CSS class to force the inline datepicker to have width 100% of its parent, so when you put the datepicker on a .col, for example, the widget adjust to the width of its parent when row width changes, up to a limit of course, but is acceptable. This was usefull for me to use inside a bootstrap .col and width will responsive.
<div style="width:30vw;">
<div id="mydatepicker"></div>
</div>
<script type="javascript">
$('#mydatepicker').datepicker();
</script>
<style>
.ui-datepicker.ui-datepicker-inline {
width: 100% !important;
}
</style>
Of course, you can put the style inside your site CSS file, but always after jQuery-ui styles.
Test with the snippet changing the width of #mydpcontainer
$('#mydatepicker').datepicker();
.ui-datepicker.ui-datepicker-inline {
width: 100% !important;
}
.mydpcontainer {
width: 35vw;
margin-left: auto;
margin-right: auto;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div class="mydpcontainer">
<div id="mydatepicker"></div>
</div>
.container {
width: 270px;
}
<div class="container">
<div class='col-md-5'>
<div class="form-group">
<div class='input-group date' id='datetimepicker6'>
<input type='text' class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>
</div>

simple jQuery UI drag and drop example

I'm trying to enable each of the list items classed "draggable" to be drag enabled for dropping into the #content div. However, the first problem is that I'm not getting an alert on the click handler. What am I missing?
<script type="text/javascript" src="<?php echo plugins_url('', __FILE__)?>/jquery-ui.js"></script>
<script type="text/javascript">
jQuery('.draggable').click
(
function()
{
alert('clicked'); //DOES NOT FIRE
jQuery(this).draggable();
jQuery( "#content" ).droppable
({
drop: function( event, ui )
{
//drop/paste jQuery(this).text();
}
});
});
</script>
<ul class="keywords">
<li class="draggable" id="kw-1">keyword one</li>
<li class="draggable" id="kw-2">keyword two</li>
<li class="draggable" id="kw-3">keyword three</li>
</ul>
Can you check if your jquery is loaded correctly? Your script only shows that you are loading jquery-ui.js.
Otherwise, your code works when both jquery and jquery-ui are loaded.

jQuery toggleClass and slideToggle

I'm toggling a class that hides and unhides a div within a list item, but I'm having problems applying a slideToggle to it.
Here is the HTML and CSS;
<style>
.collapsed .widget-content {
display:none !important;
}
</style>
<ul id="column1" class="column">
<li class="widget color-green" id="intro">
<div class="widget-head">
<a class="collapse" href"#">collapse</a>
<h3>The Title</h3>
</div>
<div class="widget-content">
<p>content here</p>
</div>
</li>
</ul>
Here is the jQuery to toggle the class of the li on click on the link with the class "collapse"
$(this).parents(settings.widgetSelector).toggleClass('collapsed');
How do I apply the slideToggle so that it only slides away the "widget-content" div whilst toggling the "collapsed" class on the li at the same time?
Thanks in advance for your advice.
you could try a callback when the toggle is complete:
$(this).parents(settings.widgetSelector).slideToggle('slow', function() {
$('#intro').toggleClass('collapsed');
});
something like that?

JQuery Hover Tip

I am trying to modify the following script to show/hide the Tip only when the "?" is hovered on and not the entire "li" Block
The HTML:
<ul class="tips">
<li>
? Feature 1
<div class="tip">
<h4>Tip Title 1</h4>
<h4>Tip Q</h4>
<p>Tip A</p>
</div>
</li>
<li>
? Feature 2
<div class="tip">
<h4>Tip Title 2</h4>
<h4>Tip Q</h4>
<p>Tip A</p>
</div>
</li>
<li>
? Feature 3
<div class="tip">
<h4>Tip Title 3</h4>
<h4>Tip Q</h4>
<p>Tip A</p>
</div>
</li>
</ul>
The JQuery script
$("ul.tips li").hover(function() {
$(this).find("div").stop()
.fadeIn()
.css("display","block")
}, function() {
$(this).find("div").stop()
.fadeOut()
});
The CSS:
.tips div {
display: none;
position: absolute;
bottom: 0;
width: 100%;
height;auto;
background: #e00;
left:0;
}​​​​​​​​​
I have tried to modify the script like so
$("ul.tips li a").hover(function() {
so it targets the "a" tag but the it ends up not showing anything.
You need to end your lines of js:
$("ul.tips li a").hover(function() {
$(this).siblings("div.tip").stop()
.fadeIn()
.css("display","block"); // <-- gotta put the semi-colon
}, function() {
$(this).siblings("div.tip").stop()
.fadeOut(); //<-- here too
});
That seems unusual as it seems like it should work, but try:
$(".tooltip").hover(function() { ... });
You should also change the $(this).find("div")... to $(this).next()...
You need to make sure that you wrap your event handler in the in jQuery's document ready function:
$(document).ready(function () {
$("ul.tips li").hover(function() {
$(this).find("div").stop()
.fadeIn()
.css("display","block")
}, function() {
$(this).find("div").stop()
.fadeOut()
});
});
Your hover event won't bind to the html elements unless the html elements have been loaded into the DOM tree already. $(document).ready() delays running the JS included in the passed anonymous function until the rest of the html document is loaded and the DOM tree is ready.
More reading at: http://docs.jquery.com/Tutorials:Introducing_$(document).ready()

Resources