• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

Creating CSS Buttons (二)

发布: 2007-6-30 18:56 | 作者: admin | 来源: | 查看: 14次 | 进入软件测试论坛讨论

领测软件测试网 A CSS Button Creation Class

Introduction
In an earlier article, Creating CSS Buttons, I examined how to create graphical-appearing buttons using nothing but HTML and CSS. While this approach is fairly simple to implement - just add a few style tags and a suitable HREF and DIV tag - it is a bit mundane to have to do for each and every button, especially since you need to have a unique set of style classes and IDs for each unique button. (If you are thoroughly confused by this point, be sure you@#ve read the Creating CSS Buttons article before delving into this one...) In this article we will examine a small, but handy class to help automate the process of creating CSS buttons. Furthermore, when using a class we@#ll be creating and placing buttons much like you would in a VB project. (For more information on creating and using classes in VBScript, be sure to read Mark Lidstone@#s excellent article: Using Classes within VBScript!)

Determining the Class Properties
The first step in creating our CSS button creating class is to decide what properties makeup a CSS button. While there is literally no limit to the number of properties one could conceivably attribute to a button, for this article I am going to assume that this set of properties conveys the basic features one would like to have in a button:

CssButton Class Properties
Name Uniquely identifies each button.
BackColor Specifies the background color of the button.
BorderColor Specifies the color of the button@#s border.
Font The font to use for the button@#s label. Must be in the format: style size font-name, such as: bold 12pt arial.
FontColor The color of the font when the button is not selected.
Width The width of the button.
Text The text to display on the button.
Url The Url to take the user to when the click the button.
MouseOverColor The color to make the font when the user@#s mouse moves over the button.
This will only work for visitors using IE...


These properties describe each button. Realize that you must use a unique Name for each independent button you want on a given Web page. If you create multiple buttons with the same name, when you mouse over one button, all of those buttons will highlight.

Creating the Class Methods
Now that we@#ve looked at the properties for the CssButton class, let@#s examine the two methods we@#ll need: GenerateStyleTag() and GenerateButtonTag(). Since each CSS button needs its own classes/IDs declared in a STYLE tag, and, additionally, needs an HREF/DIV section to display the button, these two methods return the applicable STYLE tag and HTML. These methods are very simple, and can be seen below:

Public Function GenerateStyleTag()
  @#Create the STYLE tag
  Dim strStyle
  strStyle = "<STYLE TYPE=""text/css"">" & vbCrLf & _
        "<!--" & vbCrLf & _
        "#mybutton" & Name & "   {border-style: inset; " & vbCrLf & _
        "             border-color: " & BorderColor & ";" & vbCrLf & _
        "             background-color: " & BackColor & ";" & vbCrLf & _
        "             width: " & Width & ";" & vbCrLf & _
        "             text-align: center; }" & vbCrLf & vbCrLf & vbCrLf & _
        "A.buttontext" & Name & " {color: " & FontColor & "; " & vbCrLf & _
        "              text-decoration: none; " & vbCrLf & _
        "              font: " & Font & ";" & vbCrLf & _
        "              cursor: hand; }" & vbCrLf & vbCrLf & vbCrLf & _
        ".buttonover" & Name & " {color: " & MouseOverColor & ";" & vbCrLf & _
        "             text-decoration: none; " & vbCrLf & _
        "             font: " & Font & ";" & vbCrLf & _
        "             cursor: hand; }" & vbCrLf & _
        " // -->" & vbCrLf & _
        "</STYLE>"

  GenerateStyleTag = strStyle
End Function


Public Function GenerateButtonTag()
  Dim strHTML
  strHTML = "<a href=""" & Url & """ class=""buttontext" & Name & """ " & _
        "onMouseOver=""this.className=@#buttonover" & Name & "@#;"" " & _
        "onMouseOut=""this.className=@#buttontext" & Name & "@#;"">" & _
        vbCrLf & "<div id=""mybutton" & Name & """>" & vbCrLf & Text & _
        vbCrLf & "</div></a>" & vbCrLf

  GenerateButtonTag = strHTML
End Function




Creating a Button - the Whole Process
Now that we have outlined the properties in our class, along with the two needed methods, let@#s examine how we will actually create some buttons on a Web page. The first thing to do is to create an instance of our CssButton class for each CSS button that we wish to display on the page. (The complete source for the CssButton class is available in a download at the end of this article.)

A very simple example can be seen below. It creates two buttons, one that links to Yahoo! and one that links to Lycos. (Be sure to try out the live demo!)

@#... Insert declaration of CssButton class here ...

Dim btnYahoo, btnLycos
Set btnYahoo = New CssButton
Set btnLycos = New CssButton

btnYahoo.BackColor = "#aaaaaa"
btnYahoo.BorderColor = "#bbbbbb"
btnYahoo.Font = "bold 12pt Verdana"
btnYahoo.FontColor = "black"
btnYahoo.Width = "80px"
btnYahoo.MouseOverColor = "yellow"
btnYahoo.Url = "http://www.yahoo.com/"
btnYahoo.Name = "yahoo"
btnYahoo.Text = "Yahoo!"

@#Display the Yahoo button
Response.Write btnYahoo.GenerateStyleTag()
Response.Write btnYahoo.GenerateButtonTag()
Response.Write "<p> </p>"

btnLycos.BackColor = "#aaaaaa"
btnLycos.BorderColor = "#bbbbbb"
btnLycos.Font = "10pt Arial"
btnLycos.FontColor = "black"
btnLycos.Width = "70px"
btnLycos.MouseOverColor = "yellow"
btnLycos.Url = "http://www.lycos.com/"
btnLycos.Name = "lycos"
btnLycos.Text = "Lycos"

@#Display the Lycos button
Response.Write btnLycos.GenerateStyleTag()
Response.Write btnLycos.GenerateButtonTag()


[View a live demo!]

Pretty straightforward, no? To create a button, we simply create an instance of the CssButton class, set its properties, then call output the CSS/HTML of its GenerateStyleTag() and GenerateButtonTag() methods. Note that you should create all of your buttons and set all of the properties before you emit any HTML, and then display each button@#s associated style tags in the HEAD portion of the HTML document.

Hopefully with this class you@#ll find creating CSS buttons that much easier! Enjoy, and Happy Programming!

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网