吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 97|回复: 1
收起左侧

[经验求助] js脚本脚本如何为函数添加一个参数,让其能在调用的时候控制选择style?

[复制链接]
一个学渣 发表于 2024-4-23 19:29
50吾爱币
本帖最后由 一个学渣 于 2024-4-23 19:43 编辑

函数使用t.tips('5555')调用,我想为函数添加一个参数,让其能在调用的时候控制选择新建的tipsStyle2 ,该怎么写?求帮助




[JavaScript] 纯文本查看 复制代码
 

tips: function (str) {
            const t = h5Player;
            const player = t.player();
            if (!player) {
                debug.log('h5Player Tips:', str);
                return true
            }

            const parentNode = t.getTipsContainer();

            // 修复部分提示按钮位置异常问题
            const defStyle = parentNode.getAttribute('style') || '';
            let backupStyle = parentNode.getAttribute('style-backup') || '';
            if (!backupStyle) {
                parentNode.setAttribute('style-backup', defStyle || 'style-backup:none');
                backupStyle = defStyle;
            }

            const newStyleArr = backupStyle.split(';');

            const oldPosition = parentNode.getAttribute('def-position') || window.getComputedStyle(parentNode).position;
            if (parentNode.getAttribute('def-position') === null) {
                parentNode.setAttribute('def-position', oldPosition || '');
            }
            if (['static', 'inherit', 'initial', 'unset', ''].includes(oldPosition)) {
                newStyleArr.push('position: relative');
            }

            const playerBox = player.getBoundingClientRect();
            const parentNodeBox = parentNode.getBoundingClientRect();
            /* 不存在高宽时,给包裹节点一个最小高宽,才能保证提示能正常显示 */
            if (!parentNodeBox.width || !parentNodeBox.height) {
                newStyleArr.push('min-width:' + playerBox.width + 'px');
                newStyleArr.push('min-height:' + playerBox.height + 'px');
            }

            parentNode.setAttribute('style', newStyleArr.join(';'));

            const tipsSelector = '.' + t.tipsClassName;
            let tipsDom = parentNode.querySelector(tipsSelector);

            /* 提示dom未初始化的,则进行初始化 */
            if (!tipsDom) {
                t.initTips();
                tipsDom = parentNode.querySelector(tipsSelector);
                if (!tipsDom) {
                    debug.log('init h5player tips dom error...');
                    return false
                }
            }

            const style = tipsDom.style;
            tipsDom.innerText = str;

            for (var i = 0; i < 3; i++) {
                if (this.on_off[i]) clearTimeout(this.on_off[i]);
            }

            function showTips () {
                style.display = 'block';
                t.on_off[0] = setTimeout(function () {
                    style.opacity = 1;
                }, 50);
                t.on_off[1] = setTimeout(function () {
                    // 隐藏提示框和还原样式
                    style.opacity = 0;
                    style.display = 'none';
                    if (backupStyle && backupStyle !== 'style-backup:none') {
                        parentNode.setAttribute('style', backupStyle);
                    }
                }, 2000);
            }

            if (style.display === 'block') {
                style.display = 'none';
                clearTimeout(this.on_off[3]);
                t.on_off[2] = setTimeout(function () {
                    showTips();
                }, 100);
            } else {
                showTips();
            }
        },
        /* 设置提示DOM的样式 */
        initTips: function (type=1) {
            const t = h5Player;
            const parentNode = t.getTipsContainer();
            if (parentNode.querySelector('.' + t.tipsClassName)) return

            // top: 50%;
            // left: 50%;
            // transform: translate(-50%,-50%);
            //通知提示背景透明度
            //margin-left: 20px;离左边一段距离 margin-top right右侧对齐
            const tipsStyle = `
        position: absolute;
        z-index: 999999;
        font-size: ${t.fontSize || 16}px;
        padding: 5px 10px;
        margin-right: 120px;
        background: rgba(0,0,0,0.7);
        color:white;
        top: 0;
        right: 0;
        transition: all 500ms ease;
        opacity: 0;
        border-bottom-right-radius: 5px;
        display: none;
        -webkit-font-smoothing: subpixel-antialiased;
        font-family: 'microsoft yahei', Verdana, Geneva, sans-serif;
        -webkit-user-select: none;
      `;


            const tipsStyle2 = `
        position: absolute;
        z-index: 999999;
        font-size: ${t.fontSize || 16}px;
        padding: 5px 10px;
        margin-right: 120px;
        background: rgba(0,0,0,0.7);
        color:red;
        top: 0;
        right: 0;
        transition: all 500ms ease;
        opacity: 0;
        border-bottom-right-radius: 5px;
        display: none;
        -webkit-font-smoothing: subpixel-antialiased;
        font-family: 'microsoft yahei', Verdana, Geneva, sans-serif;
        -webkit-user-select: none;
      `;





            const tips = document.createElement('div');
            tips.setAttribute('style', tipsStyle);
            tips.setAttribute('class', t.tipsClassName);
            parentNode.appendChild(tips);
        },
        on_off: new Array(3),
        rotate: 0,        fps: 30,

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

zxl1227 发表于 2024-4-23 19:39
[JavaScript] 纯文本查看 复制代码
function tips(str, useSecondStyle = false) {
    // ... 其他代码不变

    const tipsStyle = useSecondStyle ? tipsStyle2 : tipsStyle;

    // ... 其他代码不变
}
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-6 08:39

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表