翻譯|使用教程|編輯:龔雪|2020-12-08 10:27:14.733|閱讀 263 次
概述:在本教程中,我們將使用HTML、CSS、jQuery和Kendo UI提供的一些組件來構建電商APP。在本教程中,我們將構建電子商務應用程序的基本版本。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
在本教程中,我們將使用HTML、CSS、jQuery和Kendo UI提供的一些組件來構建電商APP。在本教程中,我們將構建電子商務應用程序的基本版本。
從頭開始構建組件有時會非常耗時,為了方便的處理應用程序,我們將使用Kendo UI組件,這將節省大量時間。
Kendo UI是由Progress的Telerik團隊開發的JavaScript庫,它能幫助您快速構建Web應用程序的UI。Kendo UI的核心庫提供了一系列易于使用的UI組件,例如網格、文本框、數字文本框、圖表等,Kendo UI提供了流行的JavaScript庫(如jQuery,Angular,React和Vue)的組件。
要構建此應用程序,請使用以下一些工具:
雖然開發功能齊全/可擴展的電子商務應用程序可能會很麻煩,但是實現基本版本很容易創建,我們將使用HTML/CSS, jQuery和Kendo UI創建一個應用。
創建一個名為marketplace的文件夾,并在其中定義此文件夾結構:
marketplace/
css/
styles.css
js/
items.js
functions.js
index.html
對于我們的應用程序,將使用一種非常簡單的方法:
首先準備index.html文件來接收和顯示內容,在索引文件中,輸入以下代碼行:
<!-- ./index.html --> <html> <head> <title>Awesome Market</title> <link rel="stylesheet" href="css/styles.css"> <link rel="stylesheet" href=" //stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"> <link rel="stylesheet" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous"> </head> <body> <main> <header> <nav class="navbar navbar-light bg-light justify-content-between"> <a class="navbar-brand">MarketPlace</a> <div id="cart-container"> <div id="cart"> <i class="fa fa-shopping-cart openCloseCart" style="margin-right: 10px;" aria-hidden="true">cart</i> <i class="fas fa-trash-alt" id="emptyCart">Empty cart</i> </div> <span id="itemCount"></span> </div> </nav> </header> <div id="shoppingCart"> <div id="cartItemsContainer"> <h2>Items in your cart</h2> <i class="fa fa-times-circle-o fa-3x openCloseCart" aria-hidden="true"></i> <div id="cartItems"></div> <span id="cartTotal"></span> </div> </div> <div id="products" class="row"> </div> </main> <script src="http://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"> </script> <script src="js/functions.js"></script> </body> </html>
到目前為止,我們已經定義了一個樣式表和一個包含空內容的函數文件,繼續填充它們。
打開您的CSS文件并添加文件,并向其中添加以下代碼:
// css/styles.css main { padding: 10px 0; width: 1024px; margin: 0 auto; } #cart-container { float: right; width: 210px; position: relative; } #itemCount { position: absolute; display: none; top: -10px; left: -10px; width: 20px; height: 20px; border-radius: 50%; background: red; color: white; text-align: center; } nav { margin-bottom: 30px; nav ul { list-style: none; overflow: auto; width: 100%; background: #444444; } nav ul li { float: left; padding: 5px 20px; } nav ul li a { color: #fff; text-decoration: none; } nav ul li:hover { color: #444444; background: #cccccc; } nav ul li:hover a { color: #444444; } img { width: 100%; } .item { width: 31%; float: left; margin: 1%; } .itemText p { margin-bottom: 20px; } .price-container { margin-top: 20px; } i:hover { cursor: pointer; } #shoppingCart { top: 0; left: 0; height: 100%; width: 100%; display: none; position: absolute; z-index: 9999; background: rgba(0, 0, 0, 0.6); } #cartItemsContainer { position: relative; width: 600px; left: 50%; top: 150px; margin-left: -300px; padding: 40px; box-shadow: 0 0 10px black; background: #e9e9e9; overflow: auto; } #cartItemsContainer i { position: absolute; top: 20px; right: 20px; } #cartItemsContainer .itemDetails { overflow: auto; width: 100%; margin-bottom: 40px; } #emptyCart { display: none; } #cartItemsContainer .itemImage { float: left; width: 260px; padding: 0 40px; } #cartItemsContainer .itemText { float: right; width: 260px; padding: 0 40px; } #cartItemsContainer .itemText .price-container { margin-top: 0; } .removeItem { margin-left: 40px; } .col-sm-4 { margin-bottom: 15px; }
現在,用產品填充items.js文件,向其添加以下代碼:
// js/items.js [ { "name": "Berries", "price": 23.54, "image": "http://images.unsplash.com/photo-1488900128323-21503983a07e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=400&h=400&q=80", "description": "Sweet popsicles to help with the heat" }, { "name": "Orange", "price": 10.33, "image": "http://images.unsplash.com/photo-1504185945330-7a3ca1380535?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&h=340&w=340&q=80", "description": "Mouth watering burger. Who cares if it's healthy" }, { "name": "Lemons", "price": 12.13, "image": "http://images.unsplash.com/photo-1504382262782-5b4ece78642b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=400&h=400&q=80", "description": "Sumptuous egg sandwich" }, { "name": "Bananas", "price": 10.33, "image": "http://images.unsplash.com/photo-1478369402113-1fd53f17e8b4?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=400&h=400&q=80", "description": "A great tower of pancakes. Dig in!" }, { "name": "Apples", "price": 10.33, "image": "http://images.unsplash.com/photo-1505253304499-671c55fb57fe?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=400&h=400&q=80", "description": "Great looking Waffle to start the day" }, { "name": "Sharifa", "price": 10.33, "image": "http://images.unsplash.com/photo-1470119693884-47d3a1d1f180?ixlib=rb-1.2.1&auto=format&fit=crop&w=400&h=400&q=80", "description": "What's greater than 5 minutes with grilled corn" } ]
現在打開您的functions.js文件,并向其中添加以下代碼:
// js/functions.js function init(){ // read our array of products and display it $.getJSON( "js/items.js", function( json ) { json.forEach(function (item) { $("#products").append('<div class="col-sm-4"><div class="card">' + '<img class="card-img-top" src="' + item.image + '">' + '<div class="card-body">' + '<h5 class="card-title">' + item.name + '</h5>' + '<p class="card-text price">' + "Price: $" + item.price + '</p>' + '<p class="card-text price">' + item.description + '</p>' + '<a href="#" id="showPopupNotification" class="add btn btn-primary">Add to cart</a>' + '</div>' + '</div></div>'); }); }); } $(init);
使用jQuery:
現在,當您嘗試加載index.html時,將看到一個空白屏幕。 那是因為瀏覽器由于訪問控制源而無法讀取JSON文件。為了解決這個問題,需要使用http服務器來加載文件。
首先通過在終端中輸入以下命令來安裝http-server:
npm install -g http-server
安裝后,您就可以在系統中的任何地方使用http服務器,現在在終端中輸入以下內容來提供文件:
http-server -c-1
現在,當您在瀏覽器中訪問http:// localhost:8080時,您將看到以下內容:
這意味著已經能夠閱讀和顯示產品及其信息,接下來,我們需要編寫函數來處理從購物車中添加和刪除商品的操作。
打開functions.js并添加以下代碼:
// js/functions.js function init(){ var itemCount = 0; var priceTotal = 0; // other code stays same // Add Item to Cart $('.add').click(function (){ itemCount ++; $('#itemCount').text(itemCount).css('display', 'block'); $(this).siblings().clone().appendTo('#cartItems').append('<button class="removeItem">Remove Item</button>'); // Calculate Total Price var price = parseInt($(this).siblings().find('.price').text()); priceTotal += price; $('#cartTotal').text("Total: $" + priceTotal); }); // Hide and Show Cart Items $('.openCloseCart').click(function(){ $('#shoppingCart').toggle(); }); // Empty Cart $('#emptyCart').click(function() { itemCount = 0; priceTotal = 0; $('#itemCount').css('display', 'none'); $('#cartItems').text(''); $('#cartTotal').text("Total: $" + priceTotal); }); // Remove Item From Cart $('#shoppingCart').on('click', '.removeItem', function(){ $(this).parent().remove(); itemCount --; $('#itemCount').text(itemCount); // Remove Cost of Deleted Item from Total Price var price = parseInt($(this).siblings().find('.price').text()); priceTotal -= price; $('#cartTotal').text("Total: €" + priceTotal); if (itemCount == 0) { $('#itemCount').css('display', 'none'); } }); } $(init);
在這里,我們定義了應用程序所需的功能。
至此,我們有了一個電子商務應用程序的工作版本,通過Kendo UI添加另一項功能。
您可能已經注意到,當加載頁面時,沒有顯示空購物車。 這是因為我們最初將其設置為從CSS根本不顯示。 在本節中我們將:
我們需要先將Kendo UI導入到項目中,然后才能訪問按鈕組件。
打開index.html文件,并向其中添加以下代碼行:
// ./index.html <head> //import kendoui through the cdn in your header <link rel="stylesheet" > <link rel="stylesheet" > <link rel="stylesheet" > <link rel="stylesheet" > <script src="http://kendo.cdn.telerik.com/2019.1.220/js/kendo.all.min.js"> </script> </head>
在此文件中,我們僅通過CDN導入Kendo UI。
現在在js文件中初始化函數,打開functions.js文件,并將以下代碼添加到其中:
// js/functions.js // add inside init function $("#emptyCart").kendoButton(); //insert code in bold into this function $(document).on("click", ".add", function (){ // new code $('#emptyCart').css('display', 'inline'); //other code stays same
這里我們做兩件事:
現在,重新啟動服務器,并在瀏覽器中訪問http:// localhost:8080。 將商品添加到購物車后,您將看到按鈕顯示。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都網