Jquery implements the code for selecting effects by dragging products (self-writing)

Source: Internet
Author: User

As follows:
 

Main Page index.html:
Copy codeThe Code is as follows:
<! Doctype html>
<Html lang = "en">
<Head>
<Meta charset = "UTF-8"/>
<Title> Drag and drop </title>
<Link rel = "stylesheet" href = "main.css">
<Script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"> </script>
<Script src = "jquery-ui-1.9.0.custom.min.js"> </script>
</Head>
<Body>
<Div class = "container">
<Section id = "product">
<Ul class = "clear">
<Li data-id = "1">
<A href = "#">

<H3> <font color = "#8A2BE2"> I am the first printer </font> </A>
</Li>
<Li data-id = "2">
<A href = "#">

<H3> <font color = "# A52A2A"> I am the second printer </font> </A>
</Li>
<Li data-id = "3">
<A href = "#">

<H3> <font color = "# DEB887"> I am the third printer </font> </A>
</Li>
<Li data-id = "4">
<A href = "#">

<H3> <font color = "#5F9EA0"> I am the fourth printer </font> </A>
</Li>
<Li data-id = "5">
<A href = "#">

<H3> <font color = "#7FFF00"> I am the fifth printer </font> </A>
</Li>
<Li data-id = "6">
<A href = "#">

<H3> <font color = "# D2691E"> I am the sixth printer </font> </A>
</Li>
<Li data-id = "7">
<A href = "#">

<H3> <font color = "# 6495ED"> I am the seventh printer </font> </A>
</Li>
<Li data-id = "8">
<A href = "#">

<H3> <font color = "# 00008B"> I am the eighth printer </font> </A>
</Li>
</Ul>
</Section>
<Aside id = "sidebar">
<Div class = "basket">
<Div class = "basket_list">
<Div class = "head">
<Span class = "name"> name </span>
<Span class = "count"> quantity </span>
</Div>
<Ul>
</Ul>
</Div>
</Div>
</Aside>
</Div>
<Script>
$ (Function (){
// JQuery UI Draggable
$ ("# Product li"). draggable ({

// Brings the item back to its place when dragging is over
Revert: true,

// Once the dragging starts, we decrease the opactiy of other items
// Appending a class as we do that with CSS
Drag: function (){
$ (This). addClass ("active ");
$ (This). closest ("# product"). addClass ("active ");
},

// Removing the CSS classes once dragging is over.
Stop: function (){
$ (This). removeClass ("active"). closest ("# product"). removeClass ("active ");
}
});
// JQuery Ui Droppable
$ (". Basket"). droppable ({

// The class that will be appended to the to-be-dropped-element (basket)
ActiveClass: "active ",

// The class that will be appended once we are hovering the to-be-dropped-element (basket)
HoverClass: "hover ",

// The acceptance of the item once it touches the to-be-dropped-element basket
// For different values http://api.jqueryui.com/droppable/#option-tolerance
Tolerance: "touch ",
Drop: function (event, ui ){

Var basket = $ (this ),
Move = ui. draggable,
ItemId = basket. find ("ul li [data-id = '" + move. attr ("data-id") + "']");

// To increase the value by + 1 if the same item is already in the basket
If (itemId.html ()! = Null ){
ItemId. find ("input"). val (parseInt (itemId. find ("input"). val () + 1 );
}
Else {
// Add the dragged item to the basket
AddBasket (basket, move );

// Updating the quantity by + 1 "rather than adding it to the basket
Move. find ("input"). val (parseInt (move. find ("input"). val () + 1 );
}
}
});
// This function runs onc ean item is added to the basket
Function addBasket (basket, move ){
Basket. find ("ul"). append ('<li data-id = "' + move. attr (" data-id ") + '">'
+ '<Span class = "name">' + move. find ("h3" ).html () + '</span>'
+ '<Input class = "count" value = "1" type = "text">'
+ '<Button class = "delete"> callback </button> ');
}
// The function that is triggered once delete button is pressed
$ (". Basket ul li button. delete"). live ("click", function (){
$ (This). closest ("li"). remove ();
});
});
</Script>
</Body>
</Html>

Jquery-ui-1.9.0.custom.min.js
Main.css:
Copy codeThe Code is as follows:
/* Reset &. clear
----------------------------*/
*{
Margin: 0;
Padding: 0;
}
. Clear: before,
. Clear: after {
Content :"";
Display: table;
}
. Clear: after {clear: both}
. Clear {* zoom: 1}
/* MAIN
----------------------------*/
Body {
Font: normal 12px/1.3 arial, sans-serif;
Background-color: # eee;
}
Li {list-style: none}
A {text-decoration: none}
. Container {
Position: relative;
Width: 920px;
Margin: 30px auto;
}
. Container # product {
Position: relative;
Z-index: 2;
Float: left;
Width: 670px;
}
. Container # sidebar {
Position: relative;
Z-index: 1;
Float: right;
Width: 224px;
}
/* PRODUCTS
----------------------------*/
# Product ul {
Width: 680px;
Margin-left:-10px ;}
# Product ul li {
Position: relative;
Float: left;
Width: 150px;
Margin: 0 0 10px 10px;
Padding: 5px;
Background-color: # fff;
Border-radius: 4px;
-Webkit-box-shadow: 0 1px 2px rgba (0, 0, 0,. 2 );
Box-shadow: 0 1px 2px rgba (0, 0, 0,. 2 );
-Webkit-transition:-webkit-transform. 1 s transition;
-Moz-transition:-webkit-transform. 1 s transition;
-O-transition:-webkit-transform. 1 s transition;
-Ms-transition:-webkit-transform. 1 s transition;
Transition: transform. 1 s transition;
}
# Product ul li: hover {
Background-color: # fff8c1;
}
# Product. active ul li {
-Ms-filter: "progid: DXImageTransform. Microsoft. Alpha (Opacity = 40 )";
Filter: alpha (opacity = 40 );
Opacity:. 4;
}
# Product. active ul li. active {
Z-index: 2;
-Ms-filter: "progid: DXImageTransform. Microsoft. Alpha (Opacity = 100 )";
Filter: alpha (opacity = 100 );
Opacity: 1;
-Webkit-transform-origin: 50% 50%;
-Moz-transform-origin: 50% 50%;
-O-transform-origin: 50% 50%;
-Ms-transform-origin: 50% 50%;
Transform-origin: 50% 50%;
-Webkit-transform: scale (. 6 );
-Moz-transform: scale (. 6 );
-O-transform: scale (. 6 );
-Ms-transform: scale (. 6 );
Transform: scale (. 6 );
}
# Product ul li {
Display: block;
Color: #000
}
# Product ul li a h3 {
Margin-top: 5px;
}
# Product ul li a h3,
# Product ul li a p {
White-space: nowrap;
Overflow: hidden;
-O-text-overflow: ellipsis;
-Ms-text-overflow: ellipsis;
Text-overflow: ellipsis;
}
# Product ul li a img {width: 150px; height: 150px; display: block}
/* BASKET
----------------------------*/
. Basket {
Position: relative;
}
. Basket. basket_list {
Width: 220px;
Background-color: # fff;
Border: 2px dashed transparent;
Border-radius: 4px;
-Webkit-box-shadow: 0 1px 2px rgba (0, 0, 0,. 2 );
Box-shadow: 0 1px 2px rgba (0, 0, 0,. 2 );
}
. Basket. active. basket_list,
. Basket. hover. basket_list {border-color: # ffa0a3}
. Basket. active. basket_list {background-color: # fff8c1}
. Basket. hover. basket_list {background-color: # ffa0a3}
/*. Head */
. Basket. head {
Overflow: hidden;
Margin: 0 10px;
Height: 26px;
Line-height: 26px;
Color: #666;
Border-bottom: 1px solid # ddd;
}
. Basket. head. name {float: left}
. Basket. head. count {float: right}
/*. Head */
. Basket ul {padding-bottom: 10px}
. Basket ul li {
Position: relative;
Clear: both;
Overflow: hidden;
Margin: 0 10px;
Height: 26px;
Line-height: 32px;
Border-bottom: 1px dashed # eee;
}
. Basket ul li: hover {border-bottom-color: # ccc}
. Basket ul li span. name {
Display: block;
Float: left;
Width: 165px;
Font-weight: bold;
White-space: nowrap;
Overflow: hidden;
-O-text-overflow: ellipsis;
-Ms-text-overflow: ellipsis;
Text-overflow: ellipsis;
-Webkit-transition: width. 2 s transition;
-Moz-transition: width. 2 s transition;
-O-transition: width. 2 s transition;
-Ms-transition: width. 2 s transition;
Transition: width. 2 s transition;
}
. Basket ul li: hover span. name {width: 146px}
. Basket ul li input. count {
Float: right;
Margin: 3px 2px 0 0;
Width: 25px;
Line-height: 20px;
Text-align: center;
Border: 0;
Border-radius: 3px;
Background-color: # ddd;
}
. Basket ul li button. delete {
Position: absolute;
Right: 30px;
Top: 3px;
-Ms-filter: "progid: DXImageTransform. Microsoft. Alpha (Opacity = 0 )";
Filter: alpha (opacity = 0 );
Opacity: 0;
Width: 20px;
Line-height: 20px;
Height: 20px;
Text-align: center;
Font-size: 11px;
Border: 0;
Color: # EE5757;
Background-color: # eee;
Border-radius: 3px;
Cursor: pointer;
-Webkit-transition: opacity. 2 s transition;
-Moz-transition: opacity. 2 s transition;
-O-transition: opacity. 2 s transition;
-Ms-transition: opacity. 2 s transition;
Transition: opacity. 2 s transition;
}
. Basket ul li: hover button. delete {
-Ms-filter: "progid: DXImageTransform. Microsoft. Alpha (Opacity = 100 )";
Filter: alpha (opacity = 100 );
Opacity: 1;
}
. Basket ul li button. delete: hover {
Color: # fff;
Background-color: # ffa0a3;
}
. Basket ul li button. delete: active {
Color: # fff;
Background-color: # EE5757;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.