vue项目中全局引用jquery 、引用外部js的多种方式(外部纯函数js、外部自执行js、外部js插件),附niceScroll滚动条插件使用

vue项目中全局引用jquery :

1、“   npm install jquery --save    ” 命令安装jquery

2、在项目根目录下的 build 目录下找到webpack.base.conf.js 文件,在开头使用以下代码引入webpack,如下

var webpack = require('webpack')

3、在webpack.base.conf.js 配置文件的module.exports对象中添加一段代码如下

  plugins: [new webpack.ProvidePlugin({$: "jquery",jQuery: "jquery",jquery: "jquery","window.jQuery": "jquery"})],

4、“   npm run dev    ” 命令重新运行项目

5、在main.js文件中引入如下:

import $ from 'jquery'

6、测试:在组件中直接使用jquery选择器

<template><div class="childContainer"><div id="testJquery">这行文字的颜色将用来测试全局jquery是否引用成功</div></div>
</template><script>export default {name:"Child",data(){return {}},mounted(){$("#testJquery").css({'color':'red','font-weight':'bold'});   //也可以使用jQuery("#testJquery").css()},}
</script>

 

vue项目中引用外部js插件

 

第一种外部js文件为纯函数js文件:

项目结构如下图:

 

1、公共JavaScript函数 util.js ,注意最后要使用 export {funcName}  的形式导出需要的函数

//数字相加
function addNum(a,b){return a+b;
}// 判断数组元素是否重复
function isArrRepeat(arr) {var _arr = arr.sort();console.log(_arr)for (var i = 0; i < _arr.length; i++) {if (_arr[i] === _arr[i + 1]) {return true;}}return false;}// 判断数据类型
function type(elem) {if (elem == null) {return elem + '';}return toString.call(elem).replace(/[\[\]]/g, '').split(' ')[1].toLowerCase();}//返回浏览器版本function getExplorerInfo() {let explorer = window.navigator.userAgent.toLowerCase();// ieif (explorer.indexOf("msie") >= 0) {let ver = explorer.match(/msie ([\d.]+)/)[1];return {type: "IE",version: ver};}// firefoxelse if (explorer.indexOf("firefox") >= 0) {let ver = explorer.match(/firefox\/([\d.]+)/)[1];return {type: "Firefox",version: ver};}// Chromeelse if (explorer.indexOf("chrome") >= 0) {let ver = explorer.match(/chrome\/([\d.]+)/)[1];return {type: "Chrome",version: ver};}// Operaelse if (explorer.indexOf("opera") >= 0) {let ver = explorer.match(/opera.([\d.]+)/)[1];return {type: "Opera",version: ver};}// Safarielse if (explorer.indexOf("Safari") >= 0) {let ver = explorer.match(/version\/([\d.]+)/)[1];return {type: "Safari",version: ver};}
}//导出声明的公共函数
export {addNum, getExplorerInfo
} 

2、vue模板组件中使用引用的外部js函数, 注意要使用 import {} from '../js/path' 引用导出的js函数

<template><div class="hello"><h3 id="h">{{testMsg}}</h3><div><button @click="changeTxt()">修改dom文本内容</button><button @click="add(5,8)">计算5+8</button><button @click="getExplInfo()">获取当前浏览器信息</button></div></div>
</template><script>
import {addNum,getExplorerInfo} from '../assets/js/util.js'  //引入外部js中的函数
export default {name: 'HelloWorld',data () {return {testMsg:"原始值",}},methods:{changeTxt:function(){let that=this;that.testMsg="修改后的文本值";  //修改dom数据},add:function(a,b){let c=addNum(a,b);   //使用外部js中的函数console.log(c);},getExplInfo:function(){let info=getExplorerInfo();console.log(info);}}
}
</script>

 

 

第二种外部js是自执行匿名函数:

1、该匿名函数js根据浏览器尺寸修改字体尺寸大小

(function (doc, win) {let aa=123;console.log(aa);let docEl = doc.documentElement,resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',recalc = function () {var clientWidth = docEl.clientWidth;if (!clientWidth) return;if (clientWidth >= 840) {docEl.style.fontSize = '70px';} else if (clientWidth >= 640) {docEl.style.fontSize = '50px';} else {docEl.style.fontSize = 50 * (clientWidth / 640) + 'px';}};if (!doc.addEventListener) return;win.addEventListener(resizeEvt, recalc, false);doc.addEventListener('DOMContentLoaded', recalc, false);
})(document, window);

2、在vue组件模板中使用import引入外部自执行的js

import '../assets/js/autoDone.js'  //引入外部匿名函数

 

 

第三种外部js是封装的插件js:

这里以引用niceScroll.js滚动条插件为例

1、niceScroll.js下载网盘链接:https://pan.baidu.com/s/1p_rkxCAq8bKS7x5e_-N4Cg 
提取码:dt88 
 

2、vue组件模板使用import导入外部niceScroll.js,

<template><div class="hello"><div id="boxscroll">THIS IS A SIMPLE SCROLLABLE DIV<h2> Package Description </h2><p>Release Date: August 10, 2010</p><p>The <a title="http://www.openprinting.org/show_driver.cgi?driver=hpijs" rel="nofollow" href="http://www.openprinting.org/show_driver.cgi?driver=hpijs">HPIJS</a> driver is the free, <a title="http://hplipopensource.com/hplip-web/index.html" rel="nofollow" href="http://hplipopensource.com/hplip-web/index.html">open-source driver</a> issued by HP for their DeskJet and LaserJet printers. For most <a title="" href="http://www.linuxfoundation.org/collaborate/workgroups/openprinting/macosx/hpijs#Printers">supported printers</a>,   this driver produces output quality equivalent to the proprietary HP   drivers. In photo mode, with photo paper, the output quality is very   high, especially for the HP DeskJet 990C and later models, which   auto-detect the paper type in hardware. Photo printing is fully   supported in the newer 6- and 7-ink models.</p><p>A major advantage of using this driver over those supplied by HP is   the direct interface between HPIJS and the native CUPS spooler, which   allows printing from any printer over any available connection such as   USB, AppleTalk, TCP/IP (via LPD and IPP), HP JetDirect, and shared   windows printers via SAMBA. Additionally, this driver utilizes the   existing Mac OS X USB &quot;backend&quot; and thus does not install any software   that might interfere with standard USB operation.</p><p>Please note:</p><ul><li>HP does not provide any support for HPLIP or HPIJS on the Mac OS X platform.</li><li><a href="http://www.linuxfoundation.org/en/OpenPrinting/MacOSX/hpijs-USB">Several HP USB devices</a> may not print successfully over the standard Mac OS X USB &quot;backend&quot;. Please <a href="http://www.linuxfoundation.org/en/OpenPrinting/MacOSX/hpijs-USB">see this note</a> for more information.</li></ul><h2>Release Notes</h2><ul><li>This release fixes a problem with the PPDs that caused many job   options such as page orientation, color/grayscale mode, duplex printing,   etc. to fail.</li><li>HPIJS is HP's universal printer driver for most of their   non-PostScript printers. It comes as a part of HPLIP, HP Linux Imaging   and Printing.</li><li>The PPDs for HP printers are now sourced from the HPLIP package rather than the OpenPrinting.org database.</li><li>PPDs for printers from other manufacturers are provided by OpenPrinting.org.</li><li>Some printers are only partially supported.  Printers such as<br />HP LaserJet 1022<br />HP LaserJet p1505n<br />HP LaserJet p12014<br />HP LaserJet p2035<br />require a proprietary plug-in for full support.  This package does not contain or support such plug-ins.</li></ul><p><a name="Printer_Set_Up_Instructions" id="Printer_Set_Up_Instructions"></a></p><h2> Printer Set Up Instructions </h2><p>To add a printer queue, Leopard (Mac OS X 10.5.x) users should use   the Print &amp; Fax from System Preferences. Click on the + (plus) icon   at the lower left. A new window will open. In that window, click the   Default Browser icon at the top left. Highlight your printer in the   section below. Use the &quot;Print Using&quot; pop-up menu near the bottom of the   window to select the correct PPD for your printer and click Add.</p><p>Tiger users should open the Printer Setup Utility and click on the   Add icon at the top of the Printer List window. A new window will open.   In that window, click the Default Browser icon at the top left.   Highlight your printer in the section below. Use the &quot;Print Using&quot;   pop-up menu near the bottom of the window to select the correct PPD for   your printer and click Add.</p><p>Jaguar (OS X 10.2.x) and Panther (OS X 10.3.x) users should open   Print Center (Jaguar) or Printer Setup Utility (Panther), hold down the   Option key, and click the Add Printer button in the Print Center   toolbar. Within the setup sheet, choose Advanced from the top popup   menu; then in the &quot;Device:&quot; popup menu select your printer by name (it   should be the last item in the menu list). Finally, select the correct   PPD from the model browser and click Add.</p></div></div>
</template>
<style>
#boxscroll {padding: 40px;height: 220px;width: 300px;border: 2px solid #00F;overflow: auto;margin-bottom:20px;
}
</style><script>
import '../assets/js/jquery.niceScroll.min.js'  //引入外部js插件export default {name: 'HelloWorld',data () {return {}},mounted(){$("#boxscroll").niceScroll({cursorborder:"",cursorcolor:"#00F",boxzoom:true}); // First scrollable DIV},
}
</script>

注意:该滚动条插件依赖于jquery,所以需要先全局引用jquery

 

PS附加:niceScroll.js滚动条插件使用方法

niceScroll 配置选项:

    $("#boxscroll").niceScroll({cursorcolor: "#424242", // 改变滚动条颜色,使用16进制颜色值cursoropacitymin: 0, // 当滚动条是隐藏状态时改变透明度, 值范围 1 到 0cursoropacitymax: 1, // 当滚动条是显示状态时改变透明度, 值范围 1 到 0cursorwidth: "5px", // 滚动条的宽度,单位:便素cursorborder: "1px solid #fff", // CSS方式定义滚动条边框cursorborderradius: "5px", // 滚动条圆角(像素)zindex: "auto" | 3, // 改变滚动条的DIV的z-index值scrollspeed: 60, // 滚动速度mousescrollstep: 40, // 鼠标滚轮的滚动速度 (像素)touchbehavior: false, // 激活拖拽滚动hwacceleration: true, // 激活硬件加速boxzoom: false, // 激活放大box的内容dblclickzoom: true, // (仅当 boxzoom=true时有效)双击box时放大gesturezoom: true, // (仅 boxzoom=true 和触屏设备时有效) 激活变焦当out/in(两个手指外张或收缩)grabcursorenabled: true, // (仅当 touchbehavior=true) 显示“抓住”图标display "grab" iconautohidemode: true, // 隐藏滚动条的方式, 可用的值:// true  // 无滚动时隐藏// "cursor" | // 隐藏// false | // 不隐藏,// "leave" | // 仅在指针离开内容时隐藏// "hidden" | // 一直隐藏// "scroll", // 仅在滚动时显示background: "", // 轨道的背景颜色iframeautoresize: true, // 在加载事件时自动重置iframe大小cursorminheight: 32, // 设置滚动条的最小高度 (像素)preservenativescrolling: true, // 你可以用鼠标滚动可滚动区域的滚动条和增加鼠标滚轮事件railoffset: false, // 可以使用top/left来修正位置bouncescroll: false, // (only hw accell) 启用滚动跳跃的内容移动spacebarenabled: true, // 当按下空格时使页面向下滚动railpadding: { top: 0, right: 0, left: 0, bottom: 0 }, // 设置轨道的内间距disableoutline: true, // 当选中一个使用nicescroll的div时,chrome浏览器中禁用outlinehorizrailenabled: true, // nicescroll可以管理水平滚动railalign: right, // 对齐垂直轨道railvalign: bottom, // 对齐水平轨道enabletranslate3d: true, // nicescroll 可以使用CSS变型来滚动内容enablemousewheel: true, // nicescroll可以管理鼠标滚轮事件enablekeyboard: true, // nicescroll可以管理键盘事件smoothscroll: true, // ease动画滚动sensitiverail: true, // 单击轨道产生滚动enablemouselockapi: true, // 可以用鼠标锁定API标题 (类似对象拖动)cursorfixedheight: false, // 修正光标的高度(像素)hidecursordelay: 400, // 设置滚动条淡出的延迟时间(毫秒)directionlockdeadzone: 6, // 设定死区,为激活方向锁定(像素)nativeparentscrolling: true, // 检测内容底部便于让父级滚动enablescrollonselection: true, // 当选择文本时激活内容自动滚动cursordragspeed: 0.3, // 设置拖拽的速度rtlmode: "auto", // DIV的水平滚动从左边开始cursordragontouch: false, // 使用触屏模式来实现拖拽oneaxismousemode: "auto", // 当只有水平滚动时可以用鼠标滚轮来滚动,如果设为false则不支持水平滚动,如果设为auto支持双轴滚动scriptpath: "", // 为boxmode图片自定义路径 ("" => same script path)preventmultitouchscrolling: true // 防止多触点事件引发滚动});

注意:项目中单个组件可能会使用多个niceScroll对象时,要及时隐藏用完的nicescroll对象,加载时,需要先show,再resize。

 

1、隐藏滚动条

$("#mydiv").getNiceScroll().hide();

2、当浏览器窗口尺寸改变时候,主要重置滚动条大小

$("#mydiv").getNiceScroll().resize();

3、滚动条到指定位置

$("#mydiv").getNiceScroll(0).doScrollLeft(x, duration); // Scroll X Axis
$("#mydiv").getNiceScroll(0).doScrollTop(y, duration); // Scroll Y Axis

 

参考网址:

https://blog.csdn.net/mss359681091/article/details/52838179

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/554056.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

隐藏浏览器滚动条但内容可以滚动的3种解决方式(简单清晰)

第一种&#xff1a;使用纯css样式属性隐藏滚动条 &#xff1a;火狐浏览器的css写法不兼容 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, i…

QQ浏览器登陆微信的方法

微信是腾讯旗下第二款即时通讯软件&#xff0c;简单明了的功能界面&#xff0c;简洁的聊天窗口收获了大量的用户&#xff0c;而且强大的在线支付功能与支付宝一同为现代中国的网络发展提供了巨大的推力。微信不同于QQ可以直接使用电脑客户端登陆&#xff0c;在早期更是只有手机…

vue生命周期详解、钩子函数的调用(简单易懂)

定义&#xff1a;vue的生命周期是指vue实例从初始化创建到实例销毁的过程。期间会有8个钩子函数的调用。 vue的钩子函数图解&#xff1a; vue的钩子函数使用总结&#xff1a; 1、beforeCreate&#xff08;创建前&#xff09;:beforeCreate钩子函数&#xff0c;这个时候&#x…

QQ浏览器如何查看网站保存的密码

小编日常使用的浏览器大多是QQ浏览器&#xff0c;基于腾讯QQ下的账号同步功能给我带来了许多方便&#xff0c;其中QQ浏览器的账号密码自动保存&#xff0c;同时通过云同步可以在各个终端同步账号密码&#xff0c;所以使用QQ浏览器对常用网络浏览时可以一键免密登录&#xff0c;…

vue-router详尽:编程式导航、路由重定向、动态路由匹配、路由别名、嵌套路由、命名视图

vue-router编程式导航 在vue项目中经常用到this.$router.push() 和 this.$router.replace() 方法进行路由跳转就是编程式导航。。。 通俗理解编程式导航&#xff1a;通过操作$router实例的JavaScript代码实现路由跳转。点击 <router-link :to"..."> 等同于调用…

win10 edge默认浏览器设置更改教程

win10 edge默认浏览器设置更改教程 win10 edge默认浏览器怎么设置?在Win10中&#xff0c;默认浏览器修改已经不再像以前Win7/Win8.1那样容易&#xff0c;已经不能直接通过第三方浏览器设置直接修改&#xff0c;要用到新的“招数”&#xff0c;用户需在“设置”中手动修改。如…

ie浏览器ip代理怎么设置

核心提示&#xff1a;经常上网的朋友经常会遇到想使用代理IP上网的情况&#xff0c;但是又苦恼不知道怎么设置&#xff0c;小编这就告诉大家怎么在IE浏览器上设置代理IP上网。 ie浏览器ip代理怎么设置&#xff1f;经常上网的朋友经常会遇到想使用代理IP上网的情况&#xff0c;…

理解JavaScript中this的指向详解

this的定义和理解&#xff1a; this是JavaScript语言的一个关键字&#xff0c;它是函数运行时&#xff0c;在函数体内部自动生成的一个对象&#xff0c;只能在函数体内使用。 1、this和执行环境对象有关&#xff0c;和函数的声明无关。 var name"Tom";var Bob{name:…

如何设置PP视频允许PPAP驻留

今天继续给大家分享PP视频方面的内容。   如何设置PP视频允许PPAP驻留&#xff0c;将让下次启动时更加快速&#xff0c;下面让我们一起看看&#xff0c;是如何设置的吧 1、点击“PP视频” 2、在“PP视频”播放窗口中&#xff0c;点击“菜单”按钮 3、在弹出下拉栏中&#…

深入理解call、apply、bind(改变函数中的this指向)

在JavaScript中call、apply、bind是Function 对象自带的三个方法&#xff0c;这三个方法的主要作用是改变函数中的 this 指向&#xff0c;从而可以达到接花移木的效果。本文将对这三个方法进行详细的讲解&#xff0c;并列出几个经典应用场景。 区分&#xff1a; 1、call(objec…

360浏览器设置多标签操作步骤

360浏览器设置多标签操作步骤 360浏览器怎么设置多标签?昨天小编写了一篇如何使用ie7浏览器设置多个主页的文章&#xff0c;就有小伙伴留言给小编360浏览器能否实现这项功能?当然有!下面小编就介绍操作方法&#xff1a; 1、打开360浏览器&#xff0c;在左上角找到“工具”点…

形参和实参是什么?? shim和polyfil是什么意思??

形参和实参是什么&#xff1f;&#xff1f; 实参(argument)&#xff1a; 全称为"实际参数"是在调用时传递给函数的参数. 实参可以是常量、变量、表达式、函数等&#xff0c; 无论实参是何种类型的量&#xff0c;在进行函数调用时&#xff0c;它们都必须具有确定的值…

占内存最小的浏览器:360安全浏览器超速版推荐

占内存最小的浏览器&#xff1a;360安全浏览器超速版推荐 占内存最小的浏览器有哪些?今天小编为大家推荐的是360安全浏览器超速版。本站提供360安全浏览器超速版最新正式版及抢票专版下载。那360安全浏览器超速版怎么样?看看小编整理的资料就清楚了。 360安全浏览器超速版资…

PS人像修图,通道磨皮、高低频磨皮、滤镜磨皮、人像精修

1、快速简单美肤-----高斯模糊磨皮&#xff1a; 原理 是高斯模糊蒙版。这种磨皮方法主要是针对面部的斑点及细纹。 操作流程概括&#xff1a;复制背景图层 》【滤镜】|【模糊】|【高斯模糊】命令 》 按‘Alt’键添加图层蒙版 》 在蒙版上使用画笔涂抹出想要模糊的地方》曲线…

java基础学习笔记(一)

java开发环境搭建&#xff1a;JDK配置 java 集成开发环境IDE&#xff08;Integrated Development Environment&#xff09; 》Eclipse工具 1、官网下载后安装elipse&#xff0c;注意&#xff1a;Eclipse 安装时JDK位数和Eclpise位数必须保持一致&#xff0c;安装过程中Switch…

谷歌浏览器怎么拦截网页广告 5步解决广告困扰

众所周知&#xff0c;广告是许多门户网站的主要收入来源之一&#xff0c;但广告内容的真实性辨识难度极高&#xff0c;一时大意便会中招。值得庆幸的是&#xff0c;许多浏览器产品都内置了广告拦截功能!下面小编就以谷歌浏览器为例&#xff0c;简单介绍一下广告拦截功能的开启方…

常用矢量图有哪些格式?AI文件存储为psd分层

前言 下载的矢量图资源会发现有ai格式、cdr格式的、eps格式、icon格式&#xff0c;有的能使用Photoshop打开而有的不行。使用Photoshop保存矢量图时候选择什么格式保存都是经常遇到的问题。 常见问题及解决&#xff1a;PS打开ai格式的矢量图文件&#xff0c;你用PS打开发现只…

来自天堂的连枷

来自天堂的连枷主要内容 《来自天堂的连枷》讲述的是&#xff1a;一个农夫将角长得越来越大的两头牛卖给屠户&#xff0c;他们商定的支付方式是&#xff0c;农夫给屠夫一配克油菜籽&#xff0c;然后由屠夫点数&#xff0c;一粒油菜籽付一块金币。途中农夫掉了一粒油菜籽&#…

百度地图API实现地图应用

企业官网上需要用到地图应用&#xff0c;这里对百度地图API的使用做点笔记&#xff0c;好记性不如烂笔头。 实现地图应用的流程如下&#xff1a; 1、获取密钥&#xff1b;教程网址 https://jingyan.baidu.com/article/363872eccda8286e4aa16f4e.html 2、在html的head头部引入…

win10开机密码忘记了的解锁教程

如今Win10系统逐渐成为了主流桌面系统&#xff0c;很多时候我们为了安全起见&#xff0c;往往会给自己的电脑系统设置一个开机密码&#xff0c;这样别人就不会轻易使用自己的电脑。但有时候难免会忘记开机密码。那么遇到Win10开机密码忘了怎么办呢?今天小编就给大家说说win10开…