垂直时间轴控件,主要用来描述企业发展历程大事件,或者软件版本迭代历史等,通过时间节点和事件描述来直观的展示发展的过程,一般在web网页或者app中经常看到此类控件,尤其是公司的官网关于公司部分,着重在一些独角兽公司或者正处于蓬勃发展的公司,用来展示自己多牛逼,发展多么迅猛等。
垂直时间轴控件主要存储的数据包含两个,一个是时间节点,一个是事件描述,为了后期的拓展性,采用结构体来存放这个数据,比如后期还可能增加该事件是否属于重大事件标记,是的话则绘制的时候突出显示比如加大字号加粗,本控件的主要难点在于自动计算和排列来绘制时间和事件描述,默认采用对等分的机制来处理绘制,还有部分时间轴控件是左侧时间右侧事件描述,这个可以在源码基础上自行更改或者增加样式,为了能够展示所有的事件,本控件主体是继承自滚动条区域控件,超过高度自动产生滚动条。
#ifndef TIMEAXIS_H
#define TIMEAXIS_H
/**
* 垂直时间轴控件 作者:雨田哥(QQ:3246214072) 整理:feiyangqingyun(QQ:517216493) 2019-10-07
* 1:可设置节点边距
* 2:可设置节点高度
* 3:可设置信息边框边距
* 4:可设置信息所占高度
* 5:可设置基准颜色/线条颜色
* 6:可设置标题/信息集合
* 7:自动产生滚动条
* 8:支持字符串形式设置数据
*/
#include <QScrollArea>
class TimeAxisWidget;
#ifdef quc
#if (QT_VERSION < QT_VERSION_CHECK(5,7,0))
#include <QtDesigner/QDesignerExportWidget>
#else
#include <QtUiPlugin/QDesignerExportWidget>
#endif
class QDESIGNER_WIDGET_EXPORT TimeAxis : public QScrollArea
#else
class TimeAxis : public QScrollArea
#endif
{
Q_OBJECT
Q_PROPERTY(int itemMargin READ getItemMargin WRITE setItemMargin)
Q_PROPERTY(int itemHeight READ getItemHeight WRITE setItemHeight)
Q_PROPERTY(int infoPadding READ getInfoPadding WRITE setInfoPadding)
Q_PROPERTY(int infoHeight READ getInfoHeight WRITE setInfoHeight)
Q_PROPERTY(QColor baseColor READ getBaseColor WRITE setBaseColor)
Q_PROPERTY(QColor lineColor READ getLineColor WRITE setLineColor)
Q_PROPERTY(QString title READ getTitle WRITE setTitle)
Q_PROPERTY(QString infos READ getInfos WRITE setInfos)
public:
explicit TimeAxis(QWidget *parent=0);
private:
int itemMargin; //节点边距
int itemHeight; //节点高度
int infoPadding; //信息边距
int infoHeight; //信息高度
QColor baseColor; //基准颜色
QColor lineColor; //线条颜色
QString title; //标题
QString infos; //信息集合
//时间轴主控件
TimeAxisWidget *timeAxisWidget;
public:
int getItemMargin() const;
int getItemHeight() const;
int getInfoPadding() const;
int getInfoHeight() const;
QColor getBaseColor() const;
QColor getLineColor() const;
QString getTitle() const;
QString getInfos() const;
QSize sizeHint() const;
QSize minimumSizeHint() const;
TimeAxisWidget *getWidget();
public Q_SLOTS:
//设置节点边距+节点高度
void setItemMargin(int itemMargin);
void setItemHeight(int itemHeight);
//设置信息边距+信息高度
void setInfoPadding(int infoPadding);
void setInfoHeight(int infoHeight);
//设置基准颜色+线条颜色
void setBaseColor(const QColor &baseColor);
void setLineColor(const QColor &lineColor);
//设置标题+信息集合
void setTitle(const QString &title);
void setInfos(const QString &infos);
};
class TimeAxisWidget : public QWidget
{
Q_OBJECT
public:
//可以自行拓展其他信息
struct TimeAxisInfo {
QString time; //时间
QString info; //信息
};
explicit TimeAxisWidget(QWidget *parent=0);
protected:
void paintEvent(QPaintEvent *);
void drawTitle(QPainter *painter);
void drawLine(QPainter *painter);
void drawInfo(QPainter *painter);
void drawInfoRight(QPainter *painter, const QRectF &infoRect, int infoHeight);
void drawInfoLeft(QPainter *painter, const QRectF &infoRect, int infoHeight);
private:
int itemMargin; //节点边距
int itemHeight; //节点高度
int infoPadding; //信息边距
int infoHeight; //信息高度
QColor baseColor; //基准颜色
QColor lineColor; //线条颜色
QString title; //标题
QString infos; //信息集合
//信息集合结构体
QList<TimeAxisInfo> itemInfos;
public:
int getItemMargin() const;
int getItemHeight() const;
int getInfoPadding() const;
int getInfoHeight() const;
QColor getBaseColor() const;
QColor getLineColor() const;
QString getTitle() const;
QString getInfos() const;
QSize sizeHint() const;
QSize minimumSizeHint() const;
public Q_SLOTS:
//设置节点边距+节点高度
void setItemMargin(int itemMargin);
void setItemHeight(int itemHeight);
//设置信息边距+信息高度
void setInfoPadding(int infoPadding);
void setInfoHeight(int infoHeight);
//设置基准颜色+线条颜色
void setBaseColor(const QColor &baseColor);
void setLineColor(const QColor &lineColor);
//设置标题+信息集合
void setTitle(const QString &title);
void setInfos(const QString &infos);
//设置信息集合,结构体方式
void setItemInfos(const QList<TimeAxisInfo> &itemInfos);
};
#endif // TIMEAXIS_H
【领QT开发教程学习资料,点击下方链接莬费领取↓↓,先码住不迷路~】
点击这里:Qt资料领取(视频教程+文档+代码+项目实战)
void TimeAxisWidget::drawTitle(QPainter *painter)
{
painter->save();
QFont font;
font.setBold(true);
font.setPointSize(16);
painter->setFont(font);
painter->setPen(baseColor);
painter->drawText(itemMargin, itemMargin, width() - 2 * itemMargin, 40, Qt::AlignCenter, title);
painter->restore();
}
void TimeAxisWidget::drawLine(QPainter *painter)
{
painter->save();
painter->setPen(QPen(lineColor, 6));
int startY=itemMargin + 50;
int endY=startY + itemInfos.size() * itemHeight;
painter->drawLine(width() / 2.0, startY, width() / 2.0, endY);
painter->restore();
//设置下固定高度
this->setFixedHeight(endY + itemMargin);
}
void TimeAxisWidget::drawInfo(QPainter *painter)
{
painter->save();
painter->setPen(Qt::NoPen);
QFont font;
font.setPointSize(12);
painter->setFont(font);
int startY=itemMargin + 50;
int centerX=this->width() / 2.0;
int spacer=itemMargin + 10;
//追个绘制时间轴信息集合,偶数行左侧绘制时间右侧绘制信息
for (int i=0; i < itemInfos.size(); i++) {
painter->setBrush(Qt::white);
painter->setPen(QPen(baseColor, 2));
if (i % 2==0) {
//绘制时间
QRectF textRect(0, startY, centerX - spacer, itemHeight);
painter->drawText(textRect, Qt::AlignRight | Qt::AlignVCenter, itemInfos.at(i).time);
//绘制信息边框
QRectF infoRect(centerX + spacer, textRect.center().y() - infoHeight / 2.0, centerX - spacer - itemMargin - infoHeight / 2.0, infoHeight);
drawInfoRight(painter, infoRect, infoHeight);
//绘制信息背景
painter->setBrush(baseColor);
drawInfoRight(painter, infoRect.adjusted(infoPadding, infoPadding, 0, -infoPadding), infoHeight - infoPadding * 2);
//绘制信息文字
painter->setPen(Qt::white);
painter->drawText(infoRect.adjusted(infoPadding, infoPadding, 0, -infoPadding), Qt::AlignCenter, itemInfos.at(i).info);
} else {
//绘制时间
QRectF textRect(centerX + spacer, startY, centerX - spacer, itemHeight);
painter->drawText(centerX + spacer, startY, centerX - spacer, itemHeight, Qt::AlignLeft | Qt::AlignVCenter, itemInfos.at(i).time);
//绘制信息边框
QRectF infoRect(itemMargin + infoHeight / 2.0, textRect.center().y() - infoHeight / 2.0, centerX - spacer - itemMargin - infoHeight / 2.0, infoHeight);
drawInfoLeft(painter, infoRect, infoHeight);
//绘制信息背景
painter->setBrush(baseColor);
drawInfoLeft(painter, infoRect.adjusted(0, infoPadding, -infoPadding, -infoPadding), infoHeight - infoPadding * 2);
//绘制信息文字
painter->setPen(Qt::white);
painter->drawText(infoRect.adjusted(0, infoPadding, -infoPadding, -infoPadding), Qt::AlignCenter, itemInfos.at(i).info);
}
//绘制垂直线对应的圆
painter->setPen(Qt::NoPen);
painter->setBrush(baseColor);
painter->drawEllipse(centerX - 8, startY + itemHeight / 2.0 - 8, 16, 16);
painter->setBrush(Qt::white);
painter->drawEllipse(centerX - 4, startY + itemHeight / 2.0 - 4, 8, 8);
//Y轴往下移一个高度
startY +=itemHeight;
}
painter->restore();
}
原文链接:https://www.cnblogs.com/feiyangqingyun/p/11639987.html
【领QT开发教程学习资料,点击下方链接莬费领取↓↓,先码住不迷路~】
点击这里:「链接」
本教程中,您将通过几个简短的步骤学习如何使用 CSS 插入简写和原生 JavaScript 创建滚动到顶部按钮。
滚动到顶部按钮对于具有大量内容的网站、无限滚动的页面或具有可能导致内容滚动扩展的小屏幕的移动设备非常有用。
第一步,创建按钮
要创建滚动按钮,请使用带 href="#" 的锚标记,这会使浏览器在单击时返回页面顶部,或者您可以使用自定义 Id 返回页面的特定部分。
<a href="#">scroll-to-top</a>
第二步,按钮的位置和样式
要使按钮位置相对于视口固定,您需要在锚标记上设置位置:固定。 当元素位置固定时,它会从正常的文档流中移除,然后使用相对于视口的 top、right、bottom 和 left 属性进行定位。
虽然,有一个用于定位属性的简写,称为 inset。
Inset 的作用类似于 margin 简写,用于将 margin-top、margin-right、margin-bottom 和 margin-left 设置为一体。
句法
inset: top right bottom left
当 auto 用作 inset 的值时,它认为该值被省略。
因此,您可以使用下面的插图将按钮放置在视口的右下角。
inset: auto 2em 2em auto;
要将所有内容放在一起,请将类 scrollToTopBtn 添加到锚标记并设置按钮的样式,如下所示。
.scrollToTopBtn {
color: #f2f2f2;
background-color: #151515;
text-decoration: none;
border-radius: 25px;
position: fixed;
outline: none;
z-index: 100;
padding: 0.75em 1.5em;
inset: auto 2em 2em auto;
}
第三步,让按钮响应
现在滚动到顶部按钮的样式已正确放置,并且可以正常工作。但是有一个问题,按钮总是可见的。要解决这个问题,您需要使用 JavaScript 根据页面滚动来隐藏和显示按钮。
为此,首先,获取按钮并将其存储在变量中。
const scrollToTopBtn=document.querySelector(".scrollToTopBtn");
然后获取偏移值的文档的根元素。
const rootElement=document.documentElement;
接下来,您应该在滚动事件上注册一个事件侦听器来计算按钮的可见性状态。
const handleScroll=()=> {}
document.addEventListener("scroll", handleScroll);
每次用户滚动时都会调用 handleScroll 函数。
之后,您需要可以滚动的像素总数,并且要在 handleScroll 函数中得到它,您需要通过 clientHeight 减去 scrollHeight 以获得可以滚动的像素总数。
const scrollTotal=rootElement.scrollHeight - rootElement.clientHeight;
现在您已经拥有可以滚动的最大像素数,您需要将其除以页面已滚动的数量,以获得介于 0 和 1 之间的滚动比率。使用滚动比率,您可以调整要隐藏的位置并显示按钮。越接近 1,用户在看到按钮之前滚动的次数就越多。
if ((rootElement.scrollTop / scrollTotal) > 0.25) {
// Show the button
scrollToTopBtn.classList.add("isVisible")
} else {
// Hide the button
scrollToTopBtn.classList.remove("isVisible")
}
最后,要使其工作,首先需要添加 opacity: 0; 到 scrollToTopBtn 类以在页面加载时隐藏按钮。 然后添加类 isVisible 与 opacity: 1; 当页面滚动通过您选择的比率时,按钮。 最后但同样重要的是,添加过渡:所有 250 毫秒的缓入缓出; 为按钮设置动画的 scrollToTopBtn 类。
.scrollToTopBtn {
color: #f2f2f2;
background-color: #151515;
text-decoration: none;
border-radius: 25px;
position: fixed;
outline: none;
z-index: 100;
opacity: 0;
padding: 0.75em 1.5em;
inset: auto 2em 2em auto;
transition: all 250ms ease-in-out;
}.isVisible {
opacity: 1;
}
第四步,修复不需要的点击
当滚动到顶部按钮被隐藏时,它仍然可以被点击,这是不应该发生的。 要解决这个问题,请添加指针事件:无; 类 scrollToTopBtn 忽略点击事件并添加指针事件:自动; 类 isVisible 以在按钮可见时将点击事件带回按钮。
.scrollToTopBtn {
color: #f2f2f2;
background-color: #151515;
text-decoration: none;
border-radius: 25px;
position: fixed;
outline: none;
z-index: 100;
opacity: 0;
pointer-events: none;
padding: 0.75em 1.5em;
inset: auto 2em 2em auto;
transition: all 250ms ease-in-out;
}.isVisible {
pointer-events: auto;
opacity: 1;
}
第五步,给页面添加平滑滚动
现在滚动到顶部按钮仍然可以正常工作,您可以通过平滑滚动为您的网站添加漂亮的触感。 为此,只需添加 scroll-behavior: smooth; 到 html 标签。
html {
scroll-behavior: smooth;
}
总结
滚动到顶部按钮是一个简单而有用的功能,可以极大地改善您网站的用户体验。 在本教程中,我向您展示了如何在没有任何库的情况下使用几行代码构建滚动到顶部按钮。
天发现index写漏了一个功能,为了给用户更好的体验一般网站右边都会提供一个返回顶部的按钮,正常页面载入时不显示该按钮,当我们往下移动网页时这个按钮就显示出来了,点击这个按钮我们就可以快速的滚回到顶部位置(这里是滚动回去,2345中的是直接跳回顶部的,不怎么友好,而且返回按钮一加载网页就有了,都没动何来的返回,这里明显不合逻辑),我适当的改动了下,下面是功能演示。
?
首先我们还是来写HTML代码,还是模仿2345来做的,提供了2个按钮:留言板和回顶部,这里我用的很简单,直接用一个div包裹了2个a标签,分别在a标签中嵌套了一个图标和文字内容(用到了i标签和em标签)。
<div class="Floating"> <a href="javascript:;"><i class="fa fa-pencil-square"></i><em>留言板</em></a> <a href="javascript:;"><i class="fa fa-chevron-up"></i><em>回顶部</em></a> </div>
接下来我们给DOM元素来写样式,写样式需要给点耐心,边看边调整,这样才能获取你想要的效果了,这里我用到的是position的fixed属性:生成绝对定位的元素,相对于浏览器窗口进行定位(固定定位),然后给定一个z-index属性让他永远放在其他元素上方。颜色切换我放在了a标签,直接用:hover就可以实现,图标和内容别忘了使用display: block;将其设置成块级元素。
.Floating{ width:65px; position: fixed; z-index:600; right:20px; bottom: 280px; } .Floating a{ text-decoration: none; margin-bottom: 5px; display: block; opacity: 0.9; background-color: rgb(130,130,130); } .Floating a:hover{ background-color:#33AAFF; } .Floating a i{ font-size: 24px; } .Floating a i,.Floating a em{ padding:4px; width:56px; height:24px; text-align: center; color:rgb(253, 252, 252); z-index:5; cursor: pointer; font-style: normal; display: block; }
这样我们就得到如下图所示的内容样式了,当鼠标放上去的时候背景就变成了蓝色选中状态,移出时恢复背景色即可。前面说了当页面载入时“回顶部”按钮是隐藏的,当移动后方才显示的,所里这里我们可以v-show或者v-if来控制按钮的隐藏功能,这里我们定义了一个v-show="backFlag",默认backFlag :false。
?
接下来我们就要开始给按钮添加事件了,我给“回顶部”添加一个@click="backTop"事件,我们在methods中实现backTop这个方法,既然是通过滚动来触发事件的,所以我们在页面载入时就应该对页面进行滚动监听,并指定事件触发时要执行的函数showBtn,这样当我们滚动页面的时候就对应的触发了showBtn方法,当满足条件时显示“回顶部”按钮。
// window对象表示浏览器窗口,监听滚动事件(所有浏览器都支持window对象) mounted () { window.addEventListener('scroll', this.showBtn) //scroll 滚动事件 }, methods:{ showBtn () { // 计算距离顶部的高度,当高度大于40显示回顶部,小于40则隐藏(默认隐藏) /*获取当前页面滚动条纵坐标的位置 IE9及以上:可以使用window.pageYOffset或者document.documentElement.scrollTop , safari: window.pageYOffset 与document.body.scrollTop都可以 Chrome:谷歌浏览器只认识document.body.scrollTop;*/ let that=this let scrollTop=window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop that.scrollTop=scrollTop if (that.scrollTop > 40) { let that=this that.backFlag=true } else { that.backFlag=false } }
这个应该很好理解吧,我把注释都写在代码中了,主要思路就是获取当前滚动条纵坐标的位置,当这个值大于40时就将backFlag改成true让“回顶部”按钮显示出来即可,当当前滚动条纵坐标的位置小于40时再次隐藏“回顶部”按钮。
?
接着给“回顶部”按钮添加点击事件,在methods:{}中实现backTop()方法,这里为了有个移动的过程,我给事件添加了定时器,将当前滚动条的垂直位置减去每次移动的距离赋给当前滚动条纵坐标的位置,这样就可以实现自动滚动的效果了,当移动到顶部时别忘了清除定时器(不然牛后面就没法玩了,只要一移动就会自动滚回顶部了)。
backTop(){ // 点击返回顶部方法,计时器是为了过渡顺滑 let that=this let timer=setInterval(()=> { let speed=Math.floor(-that.scrollTop /10) //scrollTop获取元素的滚动条的垂直位置,Math.floor() 向下取整 document.documentElement.scrollTop=document.body.scrollTop=that.scrollTop + speed //document.documentElement.scrollTop 获取当前页面的滚动条纵坐标位置 if (that.scrollTop===0) { clearInterval(timer) } }, 20) },
最后别忘了销毁监听事件,使用destroyed ()方法调用销毁监听事件,这样当我们离开这个页面的时候,便会调用这个函数移除监听,释放内存,到这里我们的功能就全部实现了,是不是效果不错,有兴趣的可以试试。
*请认真填写需求信息,我们会在24小时内与您取得联系。