Table of Contents
Step1 - Photoshop
Step2 - HTML/CSS
Step3 - JavaScript(jQuery)
Step4 - CSS修改
Home Web Front-end JS Tutorial Create a dynamic gradient button with jQuery Detailed graphic tutorial_jquery

Create a dynamic gradient button with jQuery Detailed graphic tutorial_jquery

May 16, 2016 pm 06:28 PM
button

本教程分为以下三步:

Step1 - Photoshop

Step2 - HTML/CSS

Step3 - JavaScript(jQuery)

Step4 - CSS修改

 

  最终结果如下:

Create a dynamic gradient button with jQuery Detailed graphic tutorial_jquery

Step1 - Photoshop

1. 新建文件

  按钮的尺寸是100px X 80px,但由于我们需要创建一个有两种状态的CSS sprite背景图,所以我们在Photoshop中创建(Ctrl+N)一个长宽为200px X 160px的图片文件,如下图:

Create a dynamic gradient button with jQuery Detailed graphic tutorial_jquery

2. 创建参考线

  为了使绘制按钮更容易,我们创建参考线,从标尺中拉出参考线,如果你找不到标尺,可以按Ctrl+R显示,如下图:

Create a dynamic gradient button with jQuery Detailed graphic tutorial_jquery

3. 绘制形状

  选择工具面板中的矩形工具,设置圆角半径为10px,在画布上绘制形状,如下图:

Create a dynamic gradient button with jQuery Detailed graphic tutorial_jquery

4. 设置形状样式

  接上图最后一步,双击层,打开图层样式窗口,设置形状的样式,首先选择渐变叠加,设置渐变颜色从#3d3d3d到#8b8b8b,如下图:

Create a dynamic gradient button with jQuery Detailed graphic tutorial_jquery

  然后,选择“内发光”,设置混合模式为“正常”,不透明度为100%,颜色设置为#ffffff,图素大小设置为3像素,如下图:

Create a dynamic gradient button with jQuery Detailed graphic tutorial_jquery

  之后,再选择“描边”,设置大小为1像素,位置为“内部”,颜色为黑色#000000,如下图:

Create a dynamic gradient button with jQuery Detailed graphic tutorial_jquery

5. 添加字体

  输入文本,设置文字相对水平和垂直居中,字体为方正准圆简体,字号36点,加粗平滑,颜色为白色(#FFFFFF),如下图:

Create a dynamic gradient button with jQuery Detailed graphic tutorial_jquery

6. 设置字体样式

  同样的双击文字图层,打开文字图层样式,设置字体样式,点击“投影”,设置混合模式为“正常”,颜色为#3e3e3e,不透明度为100%,角度为90度,距离为1像素,大小为2像素;点击“内阴影”,设置混合模式为“正常”,颜色为#454545,不透明度为75%,角度为90度,距离为1像素,大小为2像素,如下图所示:

Create a dynamic gradient button with jQuery Detailed graphic tutorial_jquery

  至此,我们就完成链接状态下的按钮背景图,效果如下:

Create a dynamic gradient button with jQuery Detailed graphic tutorial_jquery

7. 悬停背景图

  制作鼠标悬停状态下的按钮背景图,把图层放入组内,复制组,移动,并重命名,如下图:

Create a dynamic gradient button with jQuery Detailed graphic tutorial_jquery

8.背景图属性

  修改hover背景图的样式属性,打开背景图的图层样式窗口,选择“描边”,修改边框颜色为#004d77;选择“渐变叠加”,修改渐变从#1671a3到#5baedc,如下图:

 

Create a dynamic gradient button with jQuery Detailed graphic tutorial_jquery

9. 设置字体样式

  打开字体图层样式,选择“投影”,修改投影颜色为#207aad;选择“内阴影”,修改字体颜色为#0d4f74,如下图:

Create a dynamic gradient button with jQuery Detailed graphic tutorial_jquery

10. 图层半透明

  添加图层半透明效果,按以上步骤修改,鼠标悬停背景图如下,最后是再加上一层半透明层,先添加一个200px X 40px的白色层,置顶并设置白色层的透明度为10%,如下图:

Create a dynamic gradient button with jQuery Detailed graphic tutorial_jquery

  最后,我们完成的CSS sprite背景图如下,您也可以点击下载PSD文件。

Create a dynamic gradient button with jQuery Detailed graphic tutorial_jquery


Step2 - HTML/CSS
The HTML code of the button is very simple:
Front-end file
Then pass CSS Just set the background image. The CSS code is as follows:
Copy code The code is as follows:

/*Link button style*/
.button {
width:200px;
height:80px;
display:block;
background:url(bg_button.gif) top no-repeat;
text-indent:-9999px;
}
/*Button hover style*/
.button:hover{
background:url(bg_button.gif) bottom no-repeat;
}

According to the picture we designed earlier, the length and width of the button are 200px X 80px, and the background image is a black button. This piece of CSS can achieve the first effect in our example (Pure CSS effect).

Step3 - JavaScript/jQuery

Through JavaScript, we can make the button cooler. We need to add a element based on the previous one, as The background layer displayed when the mouse is hovering, then the HTML will be modified after the DOM is loaded:

view sourceprint?front end File

 The element is completely transparent before the mouse hovers over it. When the mouse passes over it, it gradually becomes opaque to achieve a gradient effect. The animation process is as follows:

Create a dynamic gradient button with jQuery Detailed graphic tutorial_jquery

Through the above analysis, we can write the jQuery code as follows. After the DOM is loaded, add a layer to the button link as the background image when the mouse passes over it, and add a mouse hover event to the element. , when the mouse passes over, it will gradually change to opaque, and when the mouse leaves, it will gradually change to fully transparent.

Copy code The code is as follows:

//Include the text into the element , and then appended to .button
$('.jsbutton,.viewbutton,.downloadbutton').wrapInner('').css('textIndent' ,'0').each(function () {
//First set the element to be fully transparent, then add the mouse hover event
$('span.hover').css('opacity ', 0).hover(function () {
$(this).stop().fadeTo(650, 1); //Fade to opaque
}, function () {
$(this ).stop().fadeTo(650, 0); //Fade to fully transparent
});
});

At this point, we have completed the JS code, and we still need Pay attention to one step, CSS modification, see Step 4.
Step4 - CSS modification
In the example of pure CSS effect, we use the :hover pseudo-class to switch the sprite image. When we use jQuery, we introduce a layer as the mouse passes over it. Background image, so the CSS needs to be modified as follows:
Copy code The code is as follows:

/* Previous button hover style*/
.button:hover{
background:url(bg_button.gif) bottom no-repeat;
}

modified to
Copy code The code is as follows:

/*There is no need to set the :hover style, but set span .hover style*/
.button span.hover {
/*Note to use absolute positioning*/
position: absolute;
display: block;
width:200px;
height:80px;
background:url(bg_button.gif) bottom no-repeat;
text-indent:-9999px;
}

Summary
Above us Implemented a dynamic gradient button in 4 steps. In the demonstration, I also provided an extended example. You can follow along to implement one yourself, or you can download the source code to modify and customize it. Of course, do you have any good suggestions or questions? , welcome to leave me a message.
Demo addresshttp://demo.jb51.net/js/gcb_download/gradual-change-button.html
Download addresshttp://demo.jb51.net/ js/gcb_download/gcb_download.rar
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to fix Microsoft Teams white screen How to fix Microsoft Teams white screen Apr 17, 2023 pm 05:07 PM

Restart Microsoft Teams If you get a blank screen after launching Teams, a good place to start is to restart the app itself. To close and restart Microsoft Teams: Right-click the Teams icon in the notification area of ​​the taskbar and click Exit from the menu. Restart Microsoft Teams from the Start menu or desktop shortcut and see if it works. Close Microsoft Teams from Task Manager If a basic restart of the Teams process doesn't work, go into Task Manager and end the task. To close Teams from Task Manager, do the following

What is the Windows Security Button? All you need to know What is the Windows Security Button? All you need to know Apr 13, 2023 pm 10:22 PM

What is the Windows Security Button? As the name suggests, Windows Security Button is a security feature that allows you to securely access the login menu and log in to your device using a password. In this case, smartphones are definitely ahead. But Windows portable devices, such as tablets, have begun adding a Windows Security button that's more than just a way to keep unwanted users out. It also provides additional login menu options. Although if you try to find the Windows Security button on your desktop PC or laptop, you might be disappointed. why is that? Tablets vs. PCs The Windows security button is a physical button that exists on tablets

5 Ways to Disable Delivery Optimization Service in Windows 5 Ways to Disable Delivery Optimization Service in Windows May 17, 2023 am 09:31 AM

There are many reasons why you might want to disable the Delivery Optimization service on your Windows computer. However, our readers complained about not knowing the correct steps to follow. This guide discusses how to disable the Delivery Optimization service in a few steps. To learn more about services, you may want to check out our How to open services.msc guide for more information. What does Delivery Optimization Service do? Delivery Optimization Service is an HTTP downloader with cloud hosting solution. It allows Windows devices to download Windows updates, upgrades, applications and other large package files from alternative sources. Additionally, it helps reduce bandwidth consumption by allowing multiple devices in a deployment to download these packages. In addition, Windo

How to restart, force restart, and shut down iPad Mini 6 How to restart, force restart, and shut down iPad Mini 6 Apr 29, 2023 pm 12:19 PM

How to Force Restart iPad Mini 6 Force restarting iPad Mini 6 is done with a series of button presses, and it works like this: Press and release for Volume Up Press and release for Volume Down Press and release the Power/Lock button until you see Apple logo, indicating that the iPad Mini has been force restarted. That’s it. You have force restarted the iPad Mini 6! Force restart is usually used for troubleshooting reasons, such as the iPad Mini freezing, apps freezing, or some other general misbehavior. One thing to note about the procedure for force restarting the 6th generation iPad Mini is that for all other devices that have ultra-thin bezels and use

After rewriting:

How to Fix PS5 Controller Not Recognized on Windows 11 After rewriting: How to Fix PS5 Controller Not Recognized on Windows 11 May 09, 2023 pm 10:16 PM

<h3>What should I know about connecting my PS5 controller? </h3><p>As good as the DualSense controller is, there have been reports of the controller not connecting or not being detected. The easiest way to solve this problem is to connect the controller to your PC using an appropriate USB cable. </p><p>Some games natively support DualSense. In these cases, you can simply plug in the controller. But this raises other questions, like what if you don't have a USB cable or don't want to use one

How to clear the download history of Microsoft Edge browser? How to clear the download history of Microsoft Edge browser? Apr 21, 2023 am 09:34 AM

<ul><li><strong>Click to enter:</strong>ChatGPT tool plug-in navigation</li></ul><h2>Find and delete download history in Edge< /h2><p>Like other browsers, Edge has a<strong>Download

Change the power button action on Windows 11 [5 Tips] Change the power button action on Windows 11 [5 Tips] Sep 29, 2023 pm 11:29 PM

The power button can do more than shut down your PC, although this is the default action for desktop users. If you want to change the power button action in Windows 11, it's easier than you think! Keep in mind that the physical power button is different from the button in the Start menu, and the changes below won't affect the operation of the latter. Additionally, you'll find slightly different power options depending on whether it's a desktop or laptop. Why should you change the power button action in Windows 11? If you put your computer to sleep more often than you shut it down, changing the way your hardware power button (that is, the physical power button on your PC) behaves will do the trick. The same idea applies to sleep mode or simply turning off the display. Change Windows 11

How to reset your Xbox Series S or X controller How to reset your Xbox Series S or X controller Jun 03, 2023 pm 08:19 PM

The Xbox gaming console is a favorite among gamers. With the new SeriesX and SeriesS, gaming is almost a lifelike experience. The Xbox controller is your primary tool for experiencing gaming effects. Sometimes the controller connection gets cut off or some errors are encountered while trying to connect the controller to the main console. This may be due to various issues related to pairing. This can be overcome with a few simple steps. Reset your Xbox Series S or Xbox Series X controller Step 1: Press and hold the Xbox button on your controller for a few seconds to turn off the controller. Step 2: On the screen, go to Turn off controller and press button A to select that option. NOTE: If you keep pressing X

See all articles