Drag and drop issue in jquery UI - jquery-ui

Im using jqueryUI for creating quiz type questions.
When I drag and drop correct answer(first one) to droppable area, it should turn green.
Rest of options, it should turn red.
Now it is not turning our properly.. can anyone pls help me on this.?
My work:
https://codepen.io/vimalraj86/pen/poJyOQr
This is the code snippet i have used.
$( function() {
$( "#draggable" ).draggable();
$( "#droppable" ).droppable({
drop: function( event, ui ) {
$( this )
.addClass( "ui-state-highlight" )
}
});
} );
$( function() {
$( "#draggable1" ).draggable();
$( "#draggable2" ).draggable();
$( "#draggable3" ).draggable();
$( "#droppable" ).droppable({
drop: function( event, ui ) {
$( this )
.addClass( "ui-state-red" )
}
});
} );

You need a condition to help decide. You can use an if statement or some other way to determine which class should be added.
$(function() {
$("div[id^='draggable']").draggable();
$("#droppable").droppable({
drop: function(event, ui) {
if (ui.draggable.text().trim() == "C") {
$(this).addClass("ui-state-green");
} else {
$(this).addClass("ui-state-red")
}
}
});
});
#draggable,
#draggable1,
#draggable2,
#draggable3 {
width: 100px;
height: 100px;
padding: 0.5em;
margin: 10px 10px 10px 0;
border: 1px solid #ccc;
background: #fff;
float: left;
}
hr {
float: left;
width: 100%;
background: #ccc;
}
#droppable {
width: 400px;
height: 150px;
padding: 0.5em;
float: left;
margin: 10px;
border: 1px solid #ccc;
background: #fff;
}
#droppable.ui-state-green {
background: green;
}
#droppable.ui-state-red {
background: #ff0000;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.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 id="droppable" class="ui-widget-header"></div>
<hr/>
<div id="draggable" class="ui-widget-content">
<p>A</p>
</div>
<div id="draggable1" class="ui-widget-content">
<p>B</p>
</div>
<div id="draggable2" class="ui-widget-content">
<p>C</p>
</div>
<div id="draggable3" class="ui-widget-content">
<p>D</p>
</div>
<button id="reset">reset</button>

Related

How to detect when draggable hits top or bottom of containment in jQuery UI

I am working on the below demo. How can I detect when .draggable hits top and bottom of its containment?
$(function(){
var draggableRight;
var draggableWidth = $('.draggable').height();
var parentWidth = $('#parent').height();
$('.draggable').draggable({
containment: 'parent',
axis: "y",
drag: function(e, ui){
// ==> if .draggable hits top of parent change it's background red
// $(document).trigger("mouseup");
// $('.draggable').css({ background: 'red' });
// ==> if .draggable hits bottom of parent change it's background blue
// $(document).trigger("mouseup");
// $('.draggable').css({ background: 'blue' });
}
});
});
#parent {
background: khaki;
width: 400px;
height: 200px;
position: relative;
margin:20px;
padding:0px;
}
.draggable {
background: #fff;
width: 400px;
height: 50px;
margin:0px;
background: red;
}
<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 id="parent">
<div class="draggable"></div>
</div>
You will want to use the position or offset from the UI object.
Consider the following:
$(function() {
var draggableRight;
var draggableWidth = $('.draggable').height();
var parentWidth = $('#parent').height();
$('.draggable').draggable({
containment: 'parent',
axis: "y",
drag: function(e, ui) {
var p = {
top: ui.offset.top - $("#parent").offset().top,
bottom: (ui.offset.top - $("#parent").offset().top) + $(this).height()
};
$("#log").html(p.top + ", " + p.bottom);
if (p.top == 0) {
console.log("Hit Top");
}
if (p.bottom == $("#parent").height()) {
console.log("Hit Bottom");
}
}
});
});
#parent {
background: khaki;
width: 400px;
height: 200px;
position: relative;
margin: 20px;
padding: 0px;
}
.draggable {
background: #fff;
width: 400px;
height: 50px;
margin: 0px;
background: red;
}
<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 id="parent">
<div class="draggable"></div>
</div>
<div id="log"></div>

Drag and Drop, cloneable and removeable

I've just started some days ago working with JQuery UI to allow some drag and drop and sorting on my homepage. In the code shown below, i wanted to add "+" and "-" to correct the equation "1_2_3=6", so the "+" has to been dropped two times to make the equation correct.
At the moment, it nearly works perfect. I can add as many "+" and "-" as I want, I can sort them into the equation. The only problem is, that I cannot remove any "+" or "-".
Can you give me any hint, how to be able to remove the signs by moving them out of the sortable window?
Thanks for your help!
<html>
<head>
<style>
#draggable { list-style-type: none; margin: 0; padding: 0; }
#draggable li { display: inline-block; margin: 1%; padding: 1%; font-size: 10vw; text-align:center; min-width:20px; border-style: solid; border-width: medium; border-color:black; background-color:grey;}
#sortable { float:left; list-style-type: none; width:100%; }
#sortable li { display: inline-block; margin: 0; padding: 0; font-size: 10vw; text-align:center; min-width:20px;}
</style>
<script src="jquery-1.12.4.js"></script>
<script src="jquery-ui.min.js"></script>
<script>
$( function() {
$( ".clone").draggable({
cursor:"move",
revert: "invalid",
connectToSortable: '#sortable',
helper: 'clone'
});
$( "#sortable").sortable({
connectWith: "ul",
cancel: ".ui-state-disabled",
});
} );
</script>
</head>
<body>
<ul id="draggable">
<li class="clone">+</li>
<li class="clone">-</li>
</ul>
<ul id="sortable">
<li class="ui-state-disabled">1</li>
<li class="ui-state-disabled">2</li>
<li class="ui-state-disabled">3=6</li>
</ul>
</body>
</html>
You can create a div when the UI components are dropped into the div you can remove the dropped component.
Sample code.
//HTML
<div class="removeDiv">
<p>Drop to remove</p>
</div>
//Style
.removeDiv{
width:100px;
height:100px;
background-color:red;
margin-top:150px;
}
//Script
$('.removeDiv').droppable({
over: function(event, ui) {
ui.draggable.remove();
}
});
This should get your work done.
Here's a fiddle for the above code. JS Fiddle
Expanding on my comment. Your code may look something like this.
$(function() {
$(".clone").draggable({
cursor: "move",
revert: "invalid",
connectToSortable: '#sortable',
helper: 'clone'
});
$("#sortable").sortable({
connectWith: "ul",
cancel: ".ui-state-disabled",
});
$(".trash").droppable({
accept: "#sortable > li",
classes: {
"ui-droppable-hover": "ui-state-highlight"
},
drop: function(event, ui) {
deleteItem(ui.draggable);
}
});
function deleteItem($o) {
$o.fadeOut().remove();
}
});
#draggable {
list-style-type: none;
margin: 0;
padding: 0;
}
#draggable li {
display: inline-block;
margin: 1%;
padding: 1%;
font-size: 10vw;
text-align: center;
min-width: 20px;
border-style: solid;
border-width: medium;
border-color: black;
background-color: grey;
}
#sortable {
list-style-type: none;
width: 100%;
}
#sortable li {
display: inline-block;
margin: 0;
padding: 0;
font-size: 10vw;
text-align: center;
min-width: 20px;
}
.trash {
width: 50px;
height: 50px;
border: 1px solid #000;
border-radius: 6px;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.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="drag-items" style="display: block;">
<ul id="draggable">
<li class="clone">+</li>
<li class="clone">-</li>
</ul>
</div>
<div class="problem" style="display: block;">
<ul id="sortable">
<li class="ui-state-disabled">1</li>
<li class="ui-state-disabled">2</li>
<li class="ui-state-disabled">3=6</li>
</ul>
</div>
<div class="trash">
<span class="ui-icon ui-icon-trash"></span>
</div>
In this code, add a small section for items to be dragged to and when they are dropped, they are removed. Also has visual feedback.

jQuery UI resizable minimal example

Why doesn't this work?
Minimal example taken from https://jqueryui.com/resizable/
// Links to jsfiddle must be accompanied by code
https://jsfiddle.net/e0sdfuLb/
Your fiddle is not working because you haven't imported also the jQuery-UI CSS:
https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css
So here is the jsfiddle updated (and working): https://jsfiddle.net/beaver71/kbu61s3a/5/
$(document).ready( function() {
$( "#resizable" ).resizable();
});
#resizable {
width: 200px;
height: 200px;
padding: 0.5em;
background: #ccc;
}
#resizable h3 { text-align: center; margin: 0; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div id="resizable" class="ui-widget-content" >
<h3 class="ui-widget-header">Resizable</h3>
</div>

Trouble with jQuery UI draggable, droppable, and sortable combo

Seems like I can choose only 2. :)
Here's where I'm at. I can sort the droppable divs and I can drag the ui-draggable, but I cannot drop ui-draggable into the ui-widget-header divs. :/
HTML:
<div class="large-9 small-9 columns">
<div id="paginationPageDisplay" class="dropme paginationContainer ui-sortable">
<div class="droppable ui-widget-header">
<div class="droppable ui-widget-header">
</div>
</div>
<div class="large-3 small-3 columns">
<div id="paginationAdDisplay" class="paginationContainer">
<div class="adThing ui-draggable">1/4</div>
<div class="adThing ui-draggable">1/2</div>
<div class="adThing ui-draggable">Full Page</div>
</div>
</div>
CSS:
.paginationContainer {
height: 1500px;
margin-right: 1px;
border: 1px solid #CCCCCC;
}
.adThing {
display: inline-block;
width: auto;
height: auto;
padding: 8px 15px;
border: 1px solid #ccc;
text-align: center;
background: #eee;
}
.dropme {
display: inline-block;
width: auto;
height: auto;
overflow: hidden;
margin-top: 20px;
}
.droppable {
height: 120px;
width: 90px;
padding: 2px;
margin: 10px 3px;
background: #F9F9F9;
border: 1px solid #CCCCCC;
float: right;
}
JS:
$('.adThing').draggable({
cursor: 'pointer',
connectWith: '.dropme',
helper: 'clone',
opacity: 0.5,
zIndex: 10
});
$('.dropme').sortable({
connectWith: '.dropme',
cursor: 'pointer'
});
$('.droppable').droppable({
drop: function( event, ui ) {
$( this )
.addClass( "ui-state-highlight" )
.find( "p" )
.html( "Dropped!" );
}
});
$("#paginationLoadButton").click(function() {
var pageCount, i, $pageDisplay = $("#paginationPageDisplay"), pageWidget;
$pageDisplay.html('');
pageCount = $("#paginationPageCount").val();
for (i=1; i<=pageCount; i++) {
pageWidget = '<div class="droppable ui-widget-header">' + i + '<p></p></div>';
$pageDisplay.html($pageDisplay.html() + pageWidget);
}
});
The problem was that I was binding droppable on document ready but those elements are generated by the button click. I moved the droppable bind into the button click event and it's resolved.
$("#paginationLoadButton").click(function() {
var pageCount, i, $pageDisplay = $("#paginationPageDisplay"), pageWidget;
$pageDisplay.html('');
pageCount = $("#paginationPageCount").val();
for (i=1; i<=pageCount; i++) {
pageWidget = '<div class="droppable ui-widget-header">' + i + '<p></p></div>';
$pageDisplay.html($pageDisplay.html() + pageWidget);
}
$('.droppable').droppable({
drop: function( event, ui ) {
$( this )
.addClass( "ui-state-highlight" )
.find( "p" )
.html( "Dropped!" );
}
});
});

JCarousel adding blogpost instead of html

is it possible to add my blogposts to JCarousel instead of a html code? It should look like this:
=> EXAMPLE
On this site the content is just html, but I want my blogpost to show off.
Let's say I wrote 5 blogposts, so the Slider must show me the 5 blogposts.
Hope someone can help me out!
Greetings
Nico
You'll need to create a macro (xslt or razor) that will display your blog post nodes and then target them with jquery to load the jcarousel. Below is an example of a razor script that does this using the code from the example you referenced:
#using umbraco.MacroEngines
#inherits DynamicNodeContext
#try
{
<script type="text/javascript" src="http://www.fdp-bw.de/jcarousel/lib/jquery-1.2.3.pack.js"></script>
<script type="text/javascript" src="http://www.fdp-bw.de/jcarousel/lib/jquery.jcarousel.pack.js"></script>
<link rel="stylesheet" type="text/css" href="http://www.fdp-bw.de/jcarousel/lib/jquery.jcarousel.css" />
<link rel="stylesheet" type="text/css" href="http://www.fdp-bw.de/jcarousel/skins/tango/skin.css" />
<style type="text/css">
/**
* Additional styles for the controls.
*/
.jcarousel-control {
margin-bottom: 10px;
text-align: center;
}
.jcarousel-control a {
font-size: 75%;
text-decoration: none;
padding: 0 5px;
margin: 0 0 5px 0;
border: 1px solid #fff;
color: #eee;
background-color: #4088b8;
font-weight: bold;
}
.jcarousel-control a:focus,
.jcarousel-control a:active {
outline: none;
}
.jcarousel-scroll {
margin-top: 10px;
text-align: center;
}
.jcarousel-scroll form {
margin: 0;
padding: 0;
}
.jcarousel-scroll select {
font-size: 75%;
}
#mycarousel-next,
#mycarousel-prev {
cursor: pointer;
margin-bottom: -10px;
text-decoration: underline;
font-size: 11px;
}
</style>
<script type="text/javascript">
/**
* We use the initCallback callback
* to assign functionality to the controls
*/
function mycarousel_initCallback(carousel) {
jQuery('.jcarousel-control a').bind('click', function () {
carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
return false;
});
jQuery('.jcarousel-scroll select').bind('change', function () {
carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
return false;
});
jQuery('#mycarousel-next').bind('click', function () {
carousel.next();
return false;
});
jQuery('#mycarousel-prev').bind('click', function () {
carousel.prev();
return false;
});
};
// Ride the carousel...
jQuery(document).ready(function () {
jQuery("#mycarousel").jcarousel({
scroll: 1,
visible: 1,
auto: 10,
wrap: "last",
initCallback: mycarousel_initCallback,
// This tells jCarousel NOT to autobuild prev/next buttons
buttonNextHTML: null,
buttonPrevHTML: null
});
});
</script>
<div id="mycarousel" class="jcarousel-skin-tango">
<ul>
#foreach (var post in Model.AncestorOrSelf().Descendants("umbBlogPost"))
{
<li>
<h2>#post.Name</h2>
<div>#post.bodyText</div>
</li>
}
</ul>
<div class="jcarousel-control">
#for (int i = 0; i < Model.AncestorOrSelf().Descendants("umbBlogPost").Count(); i++)
{
#(i + 1)
}
</div>
</div>
}
catch (Exception e)
{
<!-- #e.ToString() -->
}
That will give you all of the posts. You may want to do some ordering and filtering and then get the top five or whatever:
var posts = Model.AncestorOrSelf().Descendants("umbBlogPost").OrderBy("PostDate desc").Take(5);

Resources