Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation


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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

              <div id="page">	
		<section id="cart" class="shopping-cart">
			<h1>Shopping Cart</h1>
			<span class="total">总价0.00</span>
		<section id="products" class="products">
			<h1>Product List</h1>
				<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>
			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;
              function addEvent(element, event, delegate ) {//监听事件
			if (typeof (window.event) != 'undefined' && element.attachEvent)
				element.attachEvent('on' + event, delegate);
				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);
				var fragment = document.createElement('span');
				fragment.setAttribute('class', 'quantity');
				fragment.innerHTML = ' x 1';
				fragment = document.createElement('span');
				fragment.setAttribute('class', 'sub-total');
			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){
                    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);			


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

				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);
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.