一种绘制有向图的方法<TSE93> - 1. 引言

本文介绍了一种四步骤的算法,首先通过网络单纯形法分配节点层级,接着使用启发式算法优化交叉,然后确定节点位置,最后计算边的控制点。该算法旨在快速生成高质量的有向图,兼顾美学原则。

一种绘制有向图的方法

翻译: http://graphviz.org/Documentation/TSE93.pdf

摘要

本文描述了一种用于绘制有向图的四步骤算法。

  • 第一步,通过使用网络单纯形算法,找到最佳等级分配。

  • 第二步,减少交叉,结合新型权重函数和局部置换的迭代启发式算法为同一层级的顶点确定次序。

  • 第三步,寻找最佳坐标,构建节点和排序辅助图。

  • 第四步,通过计算样条来画边。

该算法能够快速的画出美观的有向图。

1. 引言

绘制抽象图是一个活跃的研究领域,具有诸如程序和数据结构的可视化以及文档准备之类的应用。 本文介绍了一种在平面中绘制有向图的技术。 目标是足够快地制作高质量的图纸以进行交互使用。 这些算法是实际实现的基础[GNV1]。

1.1 美学标准

在绘制过程中,可以假设有向图具有总体流程或方向,例如从上到下(在本白皮书的大多数示例中假定)或从左到右。可以在有限自动机的手工绘图中看到这样的流程,其中流程是从初始状态到最终状态,或者是从输入到输出的数据流图中。该观察结果基于以下美学原理的用于绘制有向图的方法的集合:

  • A1 在图中公开层次结构。特别是,如果可能,将边缘对准相同的大致方向。这有助于查找定向路径并突出显示源节点和宿节点。

  • A2 避免不传达有关基础图的信息的视觉异常。例如,避免边缘交叉和急剧弯曲。

  • A3 保持边缘短。这使得更容易找到相关节点,并有助于A2.

  • A4 主张对称性和平衡性。算法中这些美学原则在某些地方占有第二位的作用。

无法同时优化所有这些美感。例如,根据A1优选的节点的布置和边缘方向可能违背A2使边缘交叉。此外,在计算上难以处理以最大程度地减少边缘交叉或查找子图具有对称性。因此,我们做出一些简化的假设,并依靠启发式算法快速运行并在常见情况下进行良好的布局。需要了解其他美学原理,请读者参考Eades和Tamassia [ET] 关于图形绘制算法的带注释书目。

1.2 问题描述

绘图算法的输入是属性图 G = (V, E) 可能带有环或者多条边。假设G是连通的,对于每个连接的组件可以单独列举。其属性如下:

xsize(v), ysize(v) 节点v外接边框的尺寸。
nodesep(G) 节点外接边框之间最小水平间隔。
randsep(G) 节点外接边框之间最小竖直间隔。
w(e) 边e的权重,通常是1。权重代表边的重要程度,它被作为保持边短小和横向对齐的依据。

算法赋予每个节点v一个矩形区域的中心位于(x(v), y(v)), 并且赋予每条边一个B-Spline控制序列点(x0(e), y0(e)),…,(xn(e), yn(e))。这些值都没有指定单位,通常用72单位/英寸的坐标系系统来实现。该布局系统会遵循美学标准A1-A4以及图本身的属性。这些约束的细节会在后面章节介绍。

对于有时间线的图形或需要高亮显示来源和终止节点,用户可以通过一种方式来限制布局。下一节会叙述算法的初始遍历将节点分配给离散等级0…Max_rank。相同等级的节点接收到相同Y坐标值。用户可以提供集合Smax, Smin, S0, S1, …,Sk 子集V。这些(可能是空集)顶点集必须共同放置在最大,最小或者相同级别。

1.3 相关工作

绘制图型使用Warfield最先提出使用启发式算法画有向图以减少交叉,Carpano、Sugiyama、Tagawa以及Toda等人也发现了类似的方法。Di Battista和Tamassia提出了一种所有边指向同一方向的平面布局有向图的算法。本文算法是基于Warfield, Sugiyama等人工作之上的。

1.4 概述

图的绘制算法分为四步, 如下面伪代码。

  • 第一步,将节点分布在离散的层级。
  • 第二步,设定层级中节点的顺序避免边的交叉。
  • 第三步,设定节点确切的布局坐标。
  • 第四步,确定绘制边曲线的控制点。
procedure draw_graph()
begin
    rank();
    ordering();
    position();
    make_splines();
end

本文的贡献在于:

  1. 使用网络单纯形算法高效的为节点指定层级
  2. 使用一种优化的启发式算法减少边交叉
  3. 为层级分配问题使用一种节点坐标计算方法
  4. 一种指定曲线函数控制点的方法

(1)和(2)最先在绘图软件dag中实现[GNV1]。进一步的工作(3)和(4)被纳入dag的替代品dot中。
图1-2和1-3是dot的布局示例及其输入文件。

图 1-2a(用时1.11秒, Sun-4/280)
digraph world_dynamics {
    size="6,6";
    S8 -> 9; S24 -> 27; S24 -> 25; S1 -> 10; S1 -> 2; S35 -> 36;
    S35 -> 43; S30 -> 31; S30 -> 33; 9 -> 42; 9 -> T1; 25 -> T1;
    25 -> 26; 27 -> T24; 2 -> 3; 2 -> 16; 2 -> 17; 2 -> T1; 2 -> 18;
    10 -> 11; 10 -> 14; 10 -> T1; 10 -> 13; 10 -> 12;
    31 -> T1; 31 -> 32; 33 -> T30; 33 -> 34; 42 -> 4; 26 -> 4;
    3 -> 4; 16 -> 15; 17 -> 19; 18 -> 29; 11 -> 4; 14 -> 15;
    37 -> 39; 37 -> 41; 37 -> 38; 37 -> 40; 13 -> 19; 12 -> 29;
    43 -> 38; 43 -> 40; 36 -> 19; 32 -> 23; 34 -> 29; 39 -> 15;
    41 -> 29; 38 -> 4; 40 -> 19; 4 -> 5; 19 -> 21; 19 -> 20;
    19 -> 28; 5 -> 6; 5 -> T35; 5 -> 23; 21 -> 22; 20 -> 15; 28 -> 29;
    6 -> 7; 15 -> T1; 22 -> 23; 22 -> T35; 29 -> T30; 7 -> T8;
    23 -> T24; 23 -> T1;
}
图 1-2b 程序文件

图1-3a. (用时0.5秒,Sun-4/28)
digraph shells {
    size="7,8";
    node [fontsize=24, shape = plaintext];
    1972 -> 1976 -> 1978 -> 1980 -> 1982 -> 1984 -> 1986 -> 1988
        -> 1990 -> future;
    node [fontsize=20, shape = box];
    { rank = same;  1976 Mashey Bourne; }
    { rank = same;  1978 Formshell csh; }
    { rank = same;  1980 esh vsh; }
    { rank = same;  1982 ksh "System-V"; }
    { rank = same;  1984 v9sh tcsh; }
    { rank = same;  1986 "ksh-i"; }
    { rank = same;  1988 KornShell Perl rc; }
    { rank = same;  1990 tcl Bash; }
    { rank = same;  "future" POSIX "ksh-POSIX"; }
    Thompson -> {Mashey Bourne csh}; csh -> tcsh;
    Bourne -> {ksh esh vsh "System-V" v9sh}; v9sh -> rc;
           {Bourne "ksh-i" KornShell} -> Bash;
    {esh vsh Formshell csh} -> ksh;
    {KornShell "System-V"} -> POSIX;
    ksh -> "ksh-i" -> KornShell -> "ksh-POSIX";
    Bourne -> Formshell;
    /* ’invisible’ edges to adjust node placement */
    edge [style=invis];
    1984 -> v9sh -> tcsh ; 1988 -> rc -> KornShell;
    Formshell -> csh; KornShell -> Perl;
}
图 1-3b 程序文件
<div class="product-item" data-category="novel"> <a href="555.html?id=1"> <img src="https://tse4-mm.cn.bing.net/th/id/OIP-C.E1l9GHWnkbNLjcnJpqTpPAHaKC?rs=1&pid=ImgDetMain" alt="产品1"> <h3>产品1:红楼梦</h3> <p>价格:$50</p> </a> <button onclick="addToCart('产品1:红楼梦', 50, 'https://tse4-mm.cn.bing.net/th/id/OIP-C.E1l9GHWnkbNLjcnJpqTpPAHaKC?rs=1&pid=ImgDetMain')">添加到购物车</button> </div> <div class="product-item" data-category="novel"> <a href="555.html?id=2"> <img src="https://ts1.tc.mm.bing.net/th/id/R-C.115539104325e27be2f0ee05ca4c080b?rik=xSg%2bwFfcC1%2b2QQ&riu=http%3a%2f%2fimages.bookuu.com%2fbook%2fC%2f01638%2f2634742-fm.jpg&ehk=H9B6G0WCUFgsQjHJE1iAtNWwpFnhmdAg30ByUapdJfY%3d&risl=&pid=ImgRaw&r=0" alt="产品2"> <h3>产品2:西游记</h3> <p>价格:$75</p> </a> <button onclick="addToCart('产品2:西游记', 75, 'https://ts1.tc.mm.bing.net/th/id/R-C.115539104325e27be2f0ee05ca4c080b?rik=xSg%2bwFfcC1%2b2QQ&riu=http%3a%2f%2fimages.bookuu.com%2fbook%2fC%2f01638%2f2634742-fm.jpg&ehk=H9B6G0WCUFgsQjHJE1iAtNWwpFnhmdAg30ByUapdJfY%3d&risl=&pid=ImgRaw&r=0')">添加到购物车</button> </div> <div class="product-item" data-category="novel"> <a href="555.html?id=3"> <img src="https://tse3-mm.cn.bing.net/th/id/OIP-C.3JrruqZy-PznYJxAHsoUdAHaKe?rs=1&pid=ImgDetMain" alt="产品3"> <h3>产品3:水浒传</h3> <p>价格:$100</p> </a> <button onclick="addToCart('产品3:水浒传', 100, 'https://tse3-mm.cn.bing.net/th/id/OIP-C.3JrruqZy-PznYJxAHsoUdAHaKe?rs=1&pid=ImgDetMain')">添加到购物车</button> </div> <div class="product-item" data-category="novel"> <a href="555.html?id=4"> <img src="https://ts1.tc.mm.bing.net/th/id/R-C.741a4e06ace8ed3a2a47d2a7fc53efa6?rik=OTex%2bIz3NhB3vw&riu=http%3a%2f%2fsdwypress.com%2fuploads%2fimage%2f2106%2f24%2f210624143125esxhc.jpg&ehk=bJB%2f6p6aW7wkHHbCiUTcnRf%2bNLwOktGxYScy28rgjwg%3d&risl=&pid=ImgRaw&r=0" alt="产品4"> <h3>产品4:三国演义</h3> <p>价格:$55</p> </a> <button onclick="addToCart('产品4:三国演义', 55, 'https://ts1.tc.mm.bing.net/th/id/R-C.741a4e06ace8ed3a2a47d2a7fc53efa6?rik=OTex%2bIz3NhB3vw&riu=http%3a%2f%2fsdwypress.com%2fuploads%2fimage%2f2106%2f24%2f210624143125esxhc.jpg&ehk=bJB%2f6p6aW7wkHHbCiUTcnRf%2bNLwOktGxYScy28rgjwg%3d&risl=&pid=ImgRaw&r=0')">添加到购物车</button> </div> <div class="product-item" data-category="novel"> <a href="555.html?id=5"> <img src="https://tse1-mm.cn.bing.net/th/id/OIP-C.4tBovpeIT27lEOj0FuFT5QHaKu?rs=1&pid=ImgDetMain" alt="产品5"> <h3>产品5:红岩</h3> <p>价格:$65</p> </a> <button onclick="addToCart('产品5:红岩', 65, 'https://tse1-mm.cn.bing.net/th/id/OIP-C.4tBovpeIT27lEOj0FuFT5QHaKu?rs=1&pid=ImgDetMain')">添加到购物车</button> </div> <div class="product-item" data-category="history"> <a href="555.html?id=6"> <img src="https://tse4-mm.cn.bing.net/th/id/OIP-C.PgSOBNz_XC3Q2b_O9Nkh-wHaLS?rs=1&pid=ImgDetMain" alt="产品6"> <h3>产品6:开放中的变迁</h3> <p>价格:$50</p> </a> <button onclick="addToCart('产品6:开放中的变迁', 50, 'https://tse4-mm.cn.bing.net/th/id/OIP-C.PgSOBNz_XC3Q2b_O9Nkh-wHaLS?rs=1&pid=ImgDetMain')">添加到购物车</button> </div> <div class="product-item" data-category="history"> <a href="555.html?id=7"> <img src="https://tse2-mm.cn.bing.net/th/id/OIP-C.1O2NDFn3NcQ5UtfKVUX9qwHaLD?rs=1&pid=ImgDetMain" alt="产品7"> <h3>产品7:中国近代史</h3> <p>价格:$55</p> </a> <button onclick="addToCart('产品7:中国近代史', 55, 'https://tse2-mm.cn.bing.net/th/id/OIP-C.1O2NDFn3NcQ5UtfKVUX9qwHaLD?rs=1&pid=ImgDetMain')">添加到购物车</button> </div> <div class="product-item" data-category="history"> <a href="555.html?id=8"> <img src="https://img3.doubanio.com/lpic/s27258223.jpg" alt="产品8"> <h3>产品8:国史大纲</h3> <p>价格:$65</p> </a> <button onclick="addToCart('产品8:国史大纲', 65, 'https://img3.doubanio.com/lpic/s27258223.jpg')">添加到购物车</button> </div> <div class="product-item" data-category="science"> <a href="555.html?id=9"> <img src="https://so1.360tres.com/t0151ae7fb7ec6d8870.jpg" alt="产品9"> <h3>产品9:时间简史</h3> <p>价格:$65</p> </a> <button onclick="addToCart('产品9:时间简史', 65, 'https://so1.360tres.com/t0151ae7fb7ec6d8870.jpg')">添加到购物车</button> </div> <div class="product-item" data-category="science"> <a href="555.html?id=10"> <img src="https://img.alicdn.com/bao/uploaded/i1/3715517208/O1CN01yttyGV237JTlFwGhn_!!0-item_pic.jpg" alt="产品10"> <h3>产品10:从一到无穷大:科学中的事实和臆测</h3> <p>价格:$65</p> </a> <button onclick="addToCart('产品10:从一到无穷大:科学中的事实和臆测', 65, 'https://img.alicdn.com/bao/uploaded/i1/3715517208/O1CN01yttyGV237JTlFwGhn_!!0-item_pic.jpg')">添加到购物车</button> </div>把这些代码改成这种格式const products = [ { id: '1', name: '产品1:红楼梦', price: 50, image: 'https://tse4-mm.cn.bing.net/th/id/OIP-C.E1l9GHWnkbNLjcnJpqTpPAHaKC?rs=1&pid=ImgDetMain', description: '这是红楼梦的详细描述...' }, { id: '2', name: '产品2:西游记', price: 75, image: 'https://ts1.tc.mm.bing.net/th/id/R-C.115539104325e27be2f0ee05ca4c080b?rik=xSg%2bwFfcC1%2b2QQ&riu=http%3a%2f%2fimages.bookuu.com%2fbook%2fC%2f01638%2f2634742-fm.jpg&ehk=H9B6G0WCUFgsQjHJE1iAtNWwpFnhmdAg30ByUapdJfY%3d&risl=&pid=ImgRaw&r=0', description: '这是西游记的详细描述...' }, // 其他商品... ];
05-23
<div class="product-item" data-category="novel"> <img src="https://ts1.tc.mm.bing.net/th/id/R-C.115539104325e27be2f0ee05ca4c080b?rik=xSg%2bwFfcC1%2b2QQ&riu=http%3a%2f%2fimages.bookuu.com%2fbook%2fC%2f01638%2f2634742-fm.jpg&ehk=H9B6G0WCUFgsQjHJE1iAtNWwpFnhmdAg30ByUapdJfY%3d&risl=&pid=ImgRaw&r=0" alt="产品2"> <h3>产品2:西游记</h3> <p>价格:$75</p> <button onclick="addToCart('产品2:西游记', 75, 'https://ts1.tc.mm.bing.net/th/id/R-C.115539104325e27be2f0ee05ca4c080b?rik=xSg%2bwFfcC1%2b2QQ&riu=http%3a%2f%2fimages.bookuu.com%2fbook%2fC%2f01638%2f2634742-fm.jpg&ehk=H9B6G0WCUFgsQjHJE1iAtNWwpFnhmdAg30ByUapdJfY%3d&risl=&pid=ImgRaw&r=0')">添加到购物车</button> </div> <div class="product-item" data-category="novel"> <img src="https://tse3-mm.cn.bing.net/th/id/OIP-C.3JrruqZy-PznYJxAHsoUdAHaKe?rs=1&pid=ImgDetMain" alt="产品3"> <h3>产品3:水浒传</h3> <p>价格:$100</p> <button onclick="addToCart('产品3:水浒传', 100, 'https://tse3-mm.cn.bing.net/th/id/OIP-C.3JrruqZy-PznYJxAHsoUdAHaKe?rs=1&pid=ImgDetMain')">添加到购物车</button> </div> <div class="product-item" data-category="novel"> <img src="https://ts1.tc.mm.bing.net/th/id/R-C.741a4e06ace8ed3a2a47d2a7fc53efa6?rik=OTex%2bIz3NhB3vw&riu=http%3a%2f%2fsdwypress.com%2fuploads%2fimage%2f2106%2f24%2f210624143125esxhc.jpg&ehk=bJB%2f6p6aW7wkHHbCiUTcnRf%2bNLwOktGxYScy28rgjwg%3d&risl=&pid=ImgRaw&r=0" alt="产品4"> <h3>产品4:三国演义</h3> <p>价格:$55</p> <button onclick="addToCart('产品4:三国演义', 55, 'https://ts1.tc.mm.bing.net/th/id/R-C.741a4e06ace8ed3a2a47d2a7fc53efa6?rik=OTex%2bIz3NhB3vw&riu=http%3a%2f%2fsdwypress.com%2fuploads%2fimage%2f2106%2f24%2f210624143125esxhc.jpg&ehk=bJB%2f6p6aW7wkHHbCiUTcnRf%2bNLwOktGxYScy28rgjwg%3d&risl=&pid=ImgRaw&r=0')">添加到购物车</button> </div> <div class="product-item" data-category="novel"> <img src="https://tse1-mm.cn.bing.net/th/id/OIP-C.4tBovpeIT27lEOj0FuFT5QHaKu?rs=1&pid=ImgDetMain" alt="产品5"> <h3>产品5:红岩</h3> <p>价格:$65</p> <button onclick="addToCart('产品5:红岩', 65, 'https://tse1-mm.cn.bing.net/th/id/OIP-C.4tBovpeIT27lEOj0FuFT5QHaKu?rs=1&pid=ImgDetMain')">添加到购物车</button> </div> <div class="product-item" data-category="history"> <img src="https://tse4-mm.cn.bing.net/th/id/OIP-C.PgSOBNz_XC3Q2b_O9Nkh-wHaLS?rs=1&pid=ImgDetMain" alt="产品6"> <h3>产品6:开放中的变迁</h3> <p>价格:$50</p> <button onclick="addToCart('产品6:开放中的变迁', 50, 'https://tse4-mm.cn.bing.net/th/id/OIP-C.PgSOBNz_XC3Q2b_O9Nkh-wHaLS?rs=1&pid=ImgDetMain')">添加到购物车</button> </div> <div class="product-item" data-category="history"> <img src="https://tse2-mm.cn.bing.net/th/id/OIP-C.1O2NDFn3NcQ5UtfKVUX9qwHaLD?rs=1&pid=ImgDetMain" alt="产品7"> <h3>产品7:中国近代史</h3> <p>价格:$55</p> <button onclick="addToCart('产品7:中国近代史', 55, 'https://tse2-mm.cn.bing.net/th/id/OIP-C.1O2NDFn3NcQ5UtfKVUX9qwHaLD?rs=1&pid=ImgDetMain')">添加到购物车</button> </div> <div class="product-item" data-category="history"> <img src="https://img3.doubanio.com/lpic/s27258223.jpg" alt="产品8"> <h3>产品8:国史大纲</h3> <p>价格:$65</p> <button onclick="addToCart('产品8:国史大纲', 65, 'https://img3.doubanio.com/lpic/s27258223.jpg')">添加到购物车</button> </div> <div class="product-item" data-category="science"> <img src="https://so1.360tres.com/t0151ae7fb7ec6d8870.jpg" alt="产品9"> <h3>产品9:时间简史</h3> <p>价格:$65</p> <button onclick="addToCart('产品9:时间简史', 65, 'https://so1.360tres.com/t0151ae7fb7ec6d8870.jpg')">添加到购物车</button> </div> <div class="product-item" data-category="science"> <img src="https://img.alicdn.com/bao/uploaded/i1/3715517208/O1CN01yttyGV237JTlFwGhn_!!0-item_pic.jpg" alt="产品10"> <h3>产品10:从一到无穷大:科学中的事实和臆测</h3> <p>价格:$65</p> <button onclick="addToCart('产品10:从一到无穷大:科学中的事实和臆测', 65, 'https://img.alicdn.com/bao/uploaded/i1/3715517208/O1CN01yttyGV237JTlFwGhn_!!0-item_pic.jpg')">添加到购物车</button> </div>把这些代码改成<div class="product-item" data-category="novel"> <a href="333.html?id=1"> <img src="https://tse4-mm.cn.bing.net/th/id/OIP-C.E1l9GHWnkbNLjcnJpqTpPAHaKC?rs=1&pid=ImgDetMain" alt="产品1"> <h3>产品1:红楼梦</h3> <p>价格:$50</p> </a> <button onclick="addToCart('产品1:红楼梦', 50, 'https://tse4-mm.cn.bing.net/th/id/OIP-C.E1l9GHWnkbNLjcnJpqTpPAHaKC?rs=1&pid=ImgDetMain')">添加到购物车</button> </div>
05-23
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>首页</title> <link rel="stylesheet" href="styles.css"> <!-- 引入网站图标 --> <link rel="icon" href="C:\Users\lenovo\Pictures\黄子\0076wjjTly1hjti34wishj30si0sijtv.jpg"> <style> body { font-family: Arial, sans-serif; margin: 0; padding: 0; } /* 导航栏 */ header { background-color: #4CAF50; color: white; padding: 10px 20px; text-align: center; } nav { display: flex; justify-content: space-between; align-items: center; } .logo a { font-size: 24px; color: white; text-decoration: none; } .nav-links { list-style-type: none; padding: 0; margin: 0; } .nav-links li { display: inline; margin-left: 10px; } .nav-links a { color: white; text-decoration: none; } .product-grid { display: flex; gap: 20px; flex-wrap: wrap; padding: 20px; } .product-item { border: 1px solid #ccc; padding: 10px; width: 200px; text-align: center; cursor: pointer; transition: transform 0.2s ease-in-out; } .product-item:hover { transform: scale(1.05); } .product-item img { max-width: 100%; height: auto; } /* 商品展示区 */ .product-grid { display: flex; gap: 20px; flex-wrap: wrap; padding: 20px; } .product-item { border: 1px solid #ccc; padding: 10px; width: 200px; text-align: center; } .product-item img { max-width: 100%; height: auto; } /* 购物车 */ #cart { margin-top: 20px; } .logo img { width: 50px; /* 设置 Logo 的宽度 */ height: auto; /* 保持宽高比例 */ } #cart-list { list-style-type: none; padding: 0; } .cart-logo { width: 20px; /* 设置小 Logo 的宽度 */ height: auto; /* 保持宽高比例 */ vertical-align: middle; /* 确保 Logo 和文字对齐 */ } #cart-list li { margin: 5px 0; } /* 页脚 */ footer { text-align: center; padding: 10px; background-color: #f9f9f9; margin-top: 20px; } </style> </head> <body> <!-- 导航栏 --> <header> <nav> <div class="logo"> <a href="index.html"> <img src="C:\Users\lenovo\Pictures\黄子\0076wjjTly1hjti34wishj30si0sijtv.jpg" alt="网站Logo"> </a> </div> <ul class="nav-links"> <li><a href="index.html">首页</a></li> <li><a href="cart.html">购物车(<span id="cart-count">0</span>) <img class="cart-logo" src="C:\Users\lenovo\Pictures\OIP-C (1).jpg" alt="购物车Logo"> </a> </a></li> </ul> </nav> </header> <!-- 商品展示区 --> <main> <section id="products"> <h2>商品列表</h2> <div class="product-grid"> <section id="products"> <h2>商品列表</h2> <div class="product-grid"> <section id="products"> <h2>商品列表</h2> <div class="product-grid"> <div class="product-item" data-category="novel"> <a href="product-333.html?id=1"> <img src="https://tse4-mm.cn.bing.net/th/id/OIP-C.E1l9GHWnkbNLjcnJpqTpPAHaKC?rs=1&pid=ImgDetMain" alt="产品1"> <h3>产品1:红楼梦</h3> <p>价格:$50</p> </a> <button onclick="addToCart('产品1:红楼梦', 50, 'https://tse4-mm.cn.bing.net/th/id/OIP-C.E1l9GHWnkbNLjcnJpqTpPAHaKC?rs=1&pid=ImgDetMain')">添加到购物车</button> </div> <div class="product-item" data-category="novel"> <img src="https://ts1.tc.mm.bing.net/th/id/R-C.115539104325e27be2f0ee05ca4c080b?rik=xSg%2bwFfcC1%2b2QQ&riu=http%3a%2f%2fimages.bookuu.com%2fbook%2fC%2f01638%2f2634742-fm.jpg&ehk=H9B6G0WCUFgsQjHJE1iAtNWwpFnhmdAg30ByUapdJfY%3d&risl=&pid=ImgRaw&r=0" alt="产品2"> <h3>产品2:西游记</h3> <p>价格:$75</p> <button onclick="addToCart('产品2:西游记', 75, 'https://ts1.tc.mm.bing.net/th/id/R-C.115539104325e27be2f0ee05ca4c080b?rik=xSg%2bwFfcC1%2b2QQ&riu=http%3a%2f%2fimages.bookuu.com%2fbook%2fC%2f01638%2f2634742-fm.jpg&ehk=H9B6G0WCUFgsQjHJE1iAtNWwpFnhmdAg30ByUapdJfY%3d&risl=&pid=ImgRaw&r=0')">添加到购物车</button> </div> <div class="product-item" data-category="novel"> <img src="https://tse3-mm.cn.bing.net/th/id/OIP-C.3JrruqZy-PznYJxAHsoUdAHaKe?rs=1&pid=ImgDetMain" alt="产品3"> <h3>产品3:水浒传</h3> <p>价格:$100</p> <button onclick="addToCart('产品3:水浒传', 100, 'https://tse3-mm.cn.bing.net/th/id/OIP-C.3JrruqZy-PznYJxAHsoUdAHaKe?rs=1&pid=ImgDetMain')">添加到购物车</button> </div> <div class="product-item" data-category="novel"> <img src="https://ts1.tc.mm.bing.net/th/id/R-C.741a4e06ace8ed3a2a47d2a7fc53efa6?rik=OTex%2bIz3NhB3vw&riu=http%3a%2f%2fsdwypress.com%2fuploads%2fimage%2f2106%2f24%2f210624143125esxhc.jpg&ehk=bJB%2f6p6aW7wkHHbCiUTcnRf%2bNLwOktGxYScy28rgjwg%3d&risl=&pid=ImgRaw&r=0" alt="产品4"> <h3>产品4:三国演义</h3> <p>价格:$55</p> <button onclick="addToCart('产品4:三国演义', 55, 'https://ts1.tc.mm.bing.net/th/id/R-C.741a4e06ace8ed3a2a47d2a7fc53efa6?rik=OTex%2bIz3NhB3vw&riu=http%3a%2f%2fsdwypress.com%2fuploads%2fimage%2f2106%2f24%2f210624143125esxhc.jpg&ehk=bJB%2f6p6aW7wkHHbCiUTcnRf%2bNLwOktGxYScy28rgjwg%3d&risl=&pid=ImgRaw&r=0')">添加到购物车</button> </div> <div class="product-item" data-category="novel"> <img src="https://tse1-mm.cn.bing.net/th/id/OIP-C.4tBovpeIT27lEOj0FuFT5QHaKu?rs=1&pid=ImgDetMain" alt="产品5"> <h3>产品5:红岩</h3> <p>价格:$65</p> <button onclick="addToCart('产品5:红岩', 65, 'https://tse1-mm.cn.bing.net/th/id/OIP-C.4tBovpeIT27lEOj0FuFT5QHaKu?rs=1&pid=ImgDetMain')">添加到购物车</button> </div> <div class="product-item" data-category="history"> <img src="https://tse4-mm.cn.bing.net/th/id/OIP-C.PgSOBNz_XC3Q2b_O9Nkh-wHaLS?rs=1&pid=ImgDetMain" alt="产品6"> <h3>产品6:开放中的变迁</h3> <p>价格:$50</p> <button onclick="addToCart('产品6:开放中的变迁', 50, 'https://tse4-mm.cn.bing.net/th/id/OIP-C.PgSOBNz_XC3Q2b_O9Nkh-wHaLS?rs=1&pid=ImgDetMain')">添加到购物车</button> </div> <div class="product-item" data-category="history"> <img src="https://tse2-mm.cn.bing.net/th/id/OIP-C.1O2NDFn3NcQ5UtfKVUX9qwHaLD?rs=1&pid=ImgDetMain" alt="产品7"> <h3>产品7:中国近代史</h3> <p>价格:$55</p> <button onclick="addToCart('产品7:中国近代史', 55, 'https://tse2-mm.cn.bing.net/th/id/OIP-C.1O2NDFn3NcQ5UtfKVUX9qwHaLD?rs=1&pid=ImgDetMain')">添加到购物车</button> </div> <div class="product-item" data-category="history"> <img src="https://img3.doubanio.com/lpic/s27258223.jpg" alt="产品8"> <h3>产品8:国史大纲</h3> <p>价格:$65</p> <button onclick="addToCart('产品8:国史大纲', 65, 'https://img3.doubanio.com/lpic/s27258223.jpg')">添加到购物车</button> </div> <div class="product-item" data-category="science"> <img src="https://so1.360tres.com/t0151ae7fb7ec6d8870.jpg" alt="产品9"> <h3>产品9:时间简史</h3> <p>价格:$65</p> <button onclick="addToCart('产品9:时间简史', 65, 'https://so1.360tres.com/t0151ae7fb7ec6d8870.jpg')">添加到购物车</button> </div> <div class="product-item" data-category="science"> <img src="https://img.alicdn.com/bao/uploaded/i1/3715517208/O1CN01yttyGV237JTlFwGhn_!!0-item_pic.jpg" alt="产品10"> <h3>产品10:从一到无穷大:科学中的事实和臆测</h3> <p>价格:$65</p> <button onclick="addToCart('产品10:从一到无穷大:科学中的事实和臆测', 65, 'https://img.alicdn.com/bao/uploaded/i1/3715517208/O1CN01yttyGV237JTlFwGhn_!!0-item_pic.jpg')">添加到购物车</button> </div> </div> </section> <section id="cart"> <h2>购物车</h2> <ul id="cart-list"></ul> <p>总价:<span id="total-price">$0</span></p> <button onclick="clearCart()">清空购物车</button> <button onclick="checkout()">结算</button> </section> </main> <footer> <p>© 2023 我的电商网站</p> </footer> <script> let cart = []; // 更新购物车数量 function updateCartCount() { const count = cart.reduce((sum, item) => sum + item.quantity, 0); document.getElementById('cart-count').textContent = count; } // 添加商品到购物车 function addToCart(name, price, image) { const existingItem = cart.find(item => item.name === name); if (existingItem) { existingItem.quantity += 1; // 如果商品已存在,增加数量 } else { cart.push({ name, price, image, quantity: 1 }); // 否则新增商品 } updateCart(); // 更新购物车显示 updateCartCount(); // 更新购物车数量 } // 更新购物车显示 function updateCart() { const cartList = document.getElementById('cart-list'); const totalPriceElement = document.getElementById('total-price'); // 清空购物车列表 cartList.innerHTML = ''; let totalPrice = 0; // 遍历购物车中的商品并更新显示 cart.forEach((item, index) => { const li = document.createElement('li'); li.innerHTML = ` <img src="${item.image}" alt="${item.name}" width="50" height="50"> <span>${item.name}</span> <span>单价:$${item.price}</span> <span>数量:${item.quantity}</span> <span>小计:$${item.price * item.quantity}</span> <button onclick="removeFromCart(${index})">删除</button> `; cartList.appendChild(li); totalPrice += item.price * item.quantity; }); // 更新总价 totalPriceElement.textContent = `$${totalPrice}`; } // 从购物车中移除商品 function removeFromCart(index) { cart.splice(index, 1); // 根据索引移除商品 updateCart(); // 更新购物车显示 updateCartCount(); // 更新购物车数量 } // 清空购物车 function clearCart() { cart = []; updateCart(); // 更新购物车显示 updateCartCount(); // 更新购物车数量 } // 结算功能 function checkout() { if (cart.length === 0) { alert('购物车为空,请添加商品!'); } else { alert(`您已成功结算!总价为:$${document.getElementById('total-price').textContent}`); cart = []; updateCart(); updateCartCount(); } } </script> </body> </html>为什么还是跳转不到333.html
05-23
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值