Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="page">	
		<section id="cart" class="shopping-cart">
			<h1>Shopping Cart</h1>
			<ul>
			</ul>
			<span class="total">总价0.00</span>
		</section>
		<section id="products" class="products">
			<h1>Product List</h1>
			<ul>
				<li id="product-1" data-price="2.00"><span>Product 1</span></li>
				<li id="product-2" data-price="3.00"><span>Product 2</span></li>
				<li id="product-3" data-price="2.99"><span>Product 3</span></li>
				<li id="product-4" data-price="3.50"><span>Product 4</span></li>
				<li id="product-5" data-price="4.25"><span>Product 5</span></li>
				<li id="product-6" data-price="6.75"><span>Product 6</span></li>
				<li id="product-7" data-price="1.99"><span>Product 7</span></li>
			</ul>
		</section>
	</div> 
              
            
!

CSS

              
                li{
			list-style: none;
			margin: 10px;
			padding: 10px;
			cursor: pointer;
			width: 20%;
			text-align: center;
			display: inline-block;
			border-radius: 15px;
		}
		#products li{
			border: 1px solid #dddddd;
			box-shadow: 1px 1px 1px 1px #ccc;
		}
		#cart li{
			border: 1px solid #5635EC;
			box-shadow: 1px 1px 1px 1px #755BEC;
		}
		#cart ul{
			border-radius: 15px;
		}
		section#cart ul{
			height: 200px;
			background-color: #cccccc;
			width: 90%;
			margin: 0 auto;
		}
              
            
!

JS

              
                function addEvent(element, event, delegate ) {//监听事件
			if (typeof (window.event) != 'undefined' && element.attachEvent)
				element.attachEvent('on' + event, delegate);
			else 
				element.addEventListener(event, delegate, false);
		}
		
		addEvent(document, 'readystatechange', function() {
			if ( document.readyState !== "complete" ) 
				return true;
				
			var items = document.querySelectorAll("section.products ul li");
			var cart = document.querySelectorAll("#cart ul")[0];
			var itemsDel = document.querySelectorAll("section.shopping-cart ul li");
			function updateCart(){//更新价格
				var total = 0.0;
				var cart_items = document.querySelectorAll("#cart ul li")
				for (var i = 0; i < cart_items.length; i++) {
					var cart_item = cart_items[i];
					var quantity = cart_item.getAttribute('data-quantity');
					var price = cart_item.getAttribute('data-price');
					
					var sub_total = parseFloat(quantity * parseFloat(price));
					cart_item.querySelectorAll("span.sub-total")[0].innerHTML = " = " + sub_total.toFixed(2);
					
					total += sub_total;
				}
				
				document.querySelectorAll("#cart span.total")[0].innerHTML = "总价" + total.toFixed(2);
			}
			
			function addCartItem(item, id) {//克隆添加到购物车
				var clone = item.cloneNode(true);//复制当前节点的所有子孙节点
				clone.setAttribute('data-id', id);
				clone.setAttribute('data-quantity', 1);
				clone.removeAttribute('id');
				
				var fragment = document.createElement('span');
				fragment.setAttribute('class', 'quantity');
				fragment.innerHTML = ' x 1';
				clone.appendChild(fragment);	
				
				fragment = document.createElement('span');
				fragment.setAttribute('class', 'sub-total');
				clone.appendChild(fragment);					
				cart.appendChild(clone);
			}
			
			function updateCartItem(item){//更新个数
				var quantity = item.getAttribute('data-quantity');
				quantity = parseInt(quantity) + 1
				item.setAttribute('data-quantity', quantity);
				var span = item.querySelectorAll('span.quantity');
				span[0].innerHTML = ' x ' + quantity;
			}

            function delCartItem(item){//删除购物车数据
                var quantity = item.getAttribute('data-quantity');
                if(quantity == 1){
					cart.remove(item);
                }
                else{
                    quantity = parseInt(quantity) - 1;
                    item.setAttribute('data-quantity', quantity);
                }
            }

			function onDrop(event){			
				if(event.preventDefault) event.preventDefault();
				if (event.stopPropagation) event.stopPropagation();
				else event.cancelBubble = true;
				
				var id = event.dataTransfer.getData("Text");
				var item = document.getElementById(id);			

				console.log(id);
				console.log(item);

				var exists = document.querySelectorAll("#cart ul li[data-id='" + id + "']");
				
				if(exists.length > 0){
					updateCartItem(exists[0]);
				} else {
					addCartItem(item, id);
				}
				
				updateCart();

				return false;
			}
			
			function onDragOver(event){
				if(event.preventDefault) event.preventDefault();
				if (event.stopPropagation) event.stopPropagation();
				else event.cancelBubble = true;
				return false;
			}

			addEvent(cart, 'drop', onDrop);
			addEvent(cart, 'dragover', onDragOver);

			function onDrag(event){
				event.dataTransfer.effectAllowed = "move";
				event.dataTransfer.dropEffect = "move";
				var target = event.target || event.srcElement;
				var success = event.dataTransfer.setData('Text', target.id);
			}
				
			
			for (var i = 0; i < items.length; i++) {
				var item = items[i];
				item.setAttribute("draggable", "true");
				addEvent(item, 'dragstart', onDrag);
			};
		});
              
            
!
999px

Console