How to implement $.fn and image scrolling effects in jquery
I believe the picture scrolling effect is familiar to everyone. The rendering of Bad Street is as follows. The js implementation code is very short. However, if you want to do it, you must master the basics of jquery, IIFE, setInterval and the usage of $.fn. : I believe everyone has used the image scrolling effect. It seems to be a very simple effect. If you want to master it proficiently, you must know the basics of jquery, IIFE, setInterval and the usage of $.fn. The following article mainly introduces the $.fn in jquery. The necessary knowledge for creating fn and image scrolling effects, friends in need can refer to it.
Usage of $.fn in jquery
$.fn is the namespace of jquery. If you have studied jquery source code, it is not difficult. I found the following code in the source code:
jquery.fn=jquery.prototype={ init:function(selector,context){ /* *code */ } }
So jquery.fn is the abbreviation of jquery.prototype. The constructor jquery() instance called by our source code is actually an instance of jquery.fn.init().
The code is as follows:
jQuery = function( selector, context ) { //jqeruy内部使用new创建返回另一个构造函数实力是为了省去调用jquery时前面的new,并在后面定义了别名$; //构造函数jquery()调用的是构造函数jQuery.fn.init()的实例 return new jQuery.fn.init( selector, context ); },/*code*/
After that, the subsequent code executed jquery.fn.init.prototype=jquery.fn, and used the prototype object of the constructor jquery to overwrite jquery.fn.init() The prototype object allows the jquery.fn.init instance to also access the prototype methods and properties of jquery().
How to develop plug-ins: Use $.fn to extend jquery to generate new methods.
1. You can use jquery.extend(object) to extend the jquery class itself and add new methods to the class.
2. Use jquery.fn.extend(object) to add methods to the jquery object.
The following uses jquery.extend(object) to extend the jquery class and add class methods:
$.extent({ add: function(a,b){ return a+b; } })
You can use $.add(1,2);//3# in the future.
$.fn.extend({ [函数名]:fucntion(){ /*code*/ } });You can use $("p").function name() directly in the future. Use $.fn in jquery to encapsulate an image scrolling plug-inThis is a plug-in that is widely used. Needless to say, you know what it is. But how to implement it specifically, continue to read below. The most important part of this plug-in is the implementation of js. HTML and css are very simple and will not be described in detail. If you are already familiar with some of the following knowledge points, you can optionally skip them. setInterval()setInterval() can call the function continuously according to the specified time until clearInterval is called or the window is closed.
setInterval(fucntion(){/*code*/},[time]) clearInterval(val_of_seInterval)//参数为setInterval的返回值So when we make picture scrolling, when the mouse pointer is on the picture, we need to stop the picture scrolling. The setting here is very simple, just add an on('mouseup,mouseover',fucntion(){}) Event is enough;The specific implementation code is as follows:
var time=setInterval(picTime,par.time); /* *code */ $(this).on('mouseup,mouseover',fucntion(){ clearInterval(time); })Ensure that the picture can be scrolled continuouslyWhen designing, we definitely don’t want the picture to disappear after scrolling li.length pictures . So set a sentinel index.
var index=0; fucntion picTime(){ index++; if(index=li.length){ index=0; } showpicture(index); }Similarly, when clicking on the previous or next picture, we also need to set a sentinel so that it can continue to loop. IIFEYou definitely want the plug-in effect to be displayed immediately when the browser is loaded after the plug-in is called in the definition. Then you need to use IIFE to construct this plug-in, so as to achieve fast loading and not be interfered by other codes. Since function declaration in parentheses is invalid in js, the function enclosed by parentheses is called a function expression.
(function(){}()); (function(){})();Let’s first use a question from Niuke to understand IIFE:
var myObject = { foo: "bar", func: function() { var self = this; console.log(this.foo); console.log(self.foo); (function() { console.log(this.foo); console.log(self.foo); }()); }}; myObject.func();Because this refers to the myObject object, the first one will definitely output bar, and self is this. The variable is equal to this, so the second output is still bar. What appears below is the first IIFE form we defined above. At this time, the anonymous function must be executed immediately. Its this points to window, so the output is undefined. The last self is not defined in its own block-level scope, so the self of the parent scope is found upwards, so the fourth output is still bar. Low configuration version of picture special effects js codeMany of them have added comments: If you have a solid knowledge of jquery and js, it will definitely not be difficult.
//$()调用jquery对象 ,IIFE $(function () { $.fn.ScrollPic = function (params) { // return this.each(function () { var defaults = { ele: '.slider',//切换对象 Time: '2000',//自动切换时间 speed: '1000',//图片切换速度 scroll: true,//是否滚动图片,虽然肯定是让它滚动的,但是我们还是设置一个意思一下。 arrow: false,//是否设置箭头 number: true//是否添加右下角数字 }; //定义默认参数,其中若在html页面设置了param是,这里的params会替换defaults var par = $.extend({}, defaults, params); var scrollList = $(this).find('ul');//找到ul标签元素 var listLi = $(this).find('li');//找到li标签元素 var index = 0; var pWidth = $(this).width(); var pHeight = $(this).height(); var len = $(this).find("li").length;//
[Transfer] div+css image scrolling effect_html/css_WEB-ITnose
jQuery+css to achieve image scrolling effect (with source code)_jquery
JS small function (offsetLeft to achieve image scrolling effect) example code_javascript skills
The above is the detailed content of How to implement $.fn and image scrolling effects in jquery. For more information, please follow other related articles on the PHP Chinese website!

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Notepad++7.3.1
Easy-to-use and free code editor
