mirror of
https://gitee.com/spark-store-project/spark-web-app-runtime.git
synced 2025-12-15 03:22:05 +08:00
feat: 使用自定义 webkit 滚动条
使用自定义 webkit 滚动条,避免 QWebEngineView 原生滚动条显示(仅在 QWebEnginePage 加载时生效,部分页面动态刷新时滚动条样式不生效) Log: 使用自定义 webkit 滚动条
This commit is contained in:
@@ -1,10 +1,15 @@
|
||||
#include "webenginepage.h"
|
||||
|
||||
#include <QFile>
|
||||
#include <QWebEngineScript>
|
||||
#include <QWebEngineScriptCollection>
|
||||
#include <QDesktopServices>
|
||||
|
||||
WebEnginePage::WebEnginePage(QObject *parent)
|
||||
: QWebEnginePage(parent)
|
||||
{
|
||||
initScrollBarStyle();
|
||||
|
||||
connect(this, &QWebEnginePage::featurePermissionRequested, [&](const QUrl &origin, QWebEnginePage::Feature feature) {
|
||||
if (feature != QWebEnginePage::Notifications) {
|
||||
return;
|
||||
@@ -37,6 +42,30 @@ QWebEnginePage *WebEnginePage::createWindow(QWebEnginePage::WebWindowType type)
|
||||
return page;
|
||||
}
|
||||
|
||||
void WebEnginePage::initScrollBarStyle()
|
||||
{
|
||||
//自定义浏览器滚动条样式
|
||||
QFile file(":/css/webkit-scrollbar.css");
|
||||
if (file.open(QFile::ReadOnly)) {
|
||||
QString scrollbarStyleJS = QString("(function() {"
|
||||
" css = document.createElement('style');"
|
||||
" css.type = 'text/css';"
|
||||
" css.id = '%1';"
|
||||
" document.head.appendChild(css);"
|
||||
" css.innerText = '%2';"
|
||||
"})()\n")
|
||||
.arg("scrollbarStyle")
|
||||
.arg(QString::fromUtf8(file.readAll()).simplified());
|
||||
file.close();
|
||||
|
||||
QWebEngineScript script;
|
||||
script.setWorldId(QWebEngineScript::MainWorld);
|
||||
script.setSourceCode(scrollbarStyleJS);
|
||||
scripts().insert(script);
|
||||
runJavaScript(scrollbarStyleJS, QWebEngineScript::ApplicationWorld);
|
||||
}
|
||||
}
|
||||
|
||||
void WebEnginePage::slotUrlChanged(const QUrl &url)
|
||||
{
|
||||
if (m_currentUrl == url) {
|
||||
|
||||
Reference in New Issue
Block a user