search
HomeWeb Front-endBootstrap TutorialHow to set up a floating window in bootstrap

How to set up a floating window in bootstrap

Oct 29, 2021 am 11:25 AM
bootstrapfloating window

How to set up a floating window in bootstrap: 1. Create Html code; 2. Define a hyperlink and introduce the css and js of the corresponding page; 3. Use bootstrap's popover plug-in to achieve the floating window effect.

How to set up a floating window in bootstrap

The operating environment of this article: Windows 7 system, bootsrap version 3.3.7, DELL G3 computer

How to set up a floating window in bootstrap?

Use BootStrap to achieve the effect of floating windows

People who often play social networking sites must be familiar with such a scenario, as shown in the picture:

Hover the mouse on a hyperlink, and then a floating box will appear containing some information about the account.

I just happened to be doing some front-end stuff recently, involving similar needs. ——Hover the mouse and a floating box will appear. The floating box describes some specific information. I actually referred to an article on the Internet before, but I thought it was a bit too complicated. . And found: the magical bootstrap comes with this function.

So I used bootstrap’s popover plug-in, and the effect was pretty good. Although it is quite simple, let’s commemorate it...

Define a hyperlink, and also note that the necessary css and js for the corresponding page must be introduced:

Html code

<link href="css/bootstrap.css" rel="stylesheet" /> 
<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script> 
<script type="text/javascript" src="js/bootstrap.min.js"></script> 
<a href="#" class="bind_hover_card" data-toggle="popover" data-placement="bottom" data-trigger="hover"><img  class="commentAvatarImage lazy"  src="/static/imghwm/default1.png"  data-src="img/social_dribbble.png"    / alt="How to set up a floating window in bootstrap" ></a>

data-toggle="popover"The attribute binds the pop-up window effect to the hyperlink, data-placement="bottom"Specifies the pop-up window relative to The position where the hyperlink is displayed, data-trigger="hover" is the key, and the pop-up window will be triggered when the hyperlink is hovered. .

Some common properties about bootstrap’s popover plug-in are as follows:

Option name Type/default value Data Property name Description

animation boolean
Default value: true
data-animation To pop up The box applies a CSS fade transition effect.
#html boolean
Default value: false
data-html To pop up box to insert HTML. If false, jQuery's text method will be used to insert content into the dom. If you are concerned about XSS attacks, use text.
placement string|function
Default value: top
data-placement Specifies how to position the pop-up box (i.e. top|bottom|left|right|auto).
When specified as auto, the pop-up box will be dynamically adjusted. For example, if placement is "auto left", the popup will be displayed on the left if possible, and it will be displayed on the right if the situation does not allow it.
selector string
Default value: false
data-selector if provided If a selector is specified, the popup object will be delegated to the specified target.
title string | function
Default value: ''
data-title If the title attribute is not specified, the title option is the default title value.
trigger string
Default value: 'hover focus'
data-trigger Define how to trigger the pop-up box: click| hover | focus | manual. You can pass multiple triggers, each separated by a space.
delay number | object
默认值:0
data-delay 延迟显示和隐藏弹出框的毫秒数 - 对 manual 手动触发类型不适用。如果提供的是一个数字,那么延迟将会应用于显示和隐藏。如果提供的是对象,结构如下所示:      
delay:{ show:500, hide:100}
     
container string | false
默认值:false
data-container 向指定元素追加弹出框。
     实例: container: 'body'

常见方法:

方法描述实例Options: .popover(options)向元素集合附加弹出框句柄。

$().popover(options)
Toggle: .popover(&#39;toggle&#39;)切换显示/隐藏元素的弹出框。
$(&#39;#element&#39;).popover(&#39;toggle&#39;)
Show: .popover(&#39;show&#39;)显示元素的弹出框。
$(&#39;#element&#39;).popover(&#39;show&#39;)
Hide: .popover(&#39;hide&#39;)隐藏元素的弹出框。
$(&#39;#element&#39;).popover(&#39;hide&#39;)
Destroy: .popover(&#39;destroy&#39;)隐藏并销毁元素的弹出框。
$(&#39;#element&#39;).popover(&#39;destroy&#39;)

好了,下面重点是Js部分。

$(function() { 
  $("[data-toggle=&#39;popover&#39;]").popover({ 
    html : true,  
    title: title(),  
    delay:{show:500, hide:1000}, 
    content: function() { 
     return content();  
    }  
  }); 
});

 而我们来模拟下动态加载悬浮框的标题和内容:

//模拟动态加载标题(真实情况可能会跟后台进行ajax交互) 
function title() { 
  return &#39;田喜碧Hebe(节制的人生)&#39;; 
} 
 
//模拟动态加载内容(真实情况可能会跟后台进行ajax交互) 
function content() { 
  var data = $("<form><ul><li><span aria-hidden=&#39;true&#39; class=&#39;icon_globe&#39;></span> <font>粉丝数:</font>7389223</li>" + 
       "<li><span aria-hidden=&#39;true&#39; class=&#39;icon_piechart&#39;></span> <font>关注:</font>265</li>" + 
       "<li><span aria-hidden=&#39;true&#39; class=&#39;icon_search_alt&#39;></span> <font>微博:</font>645</li>" + 
       "<li><span aria-hidden=&#39;true&#39; class=&#39;icon_pens_alt&#39;></span> <font>所在地:</font>台湾</li>" + 
       "<input id=&#39;btn&#39; type=&#39;button&#39; value=&#39;关注&#39; onclick=&#39;test()&#39;/></form>"); 
   
  return data; 
} 
//模拟悬浮框里面的按钮点击操作 
function test() { 
  alert(&#39;关注成功&#39;); 
}

查看效果:

推荐学习:《bootstrap使用教程

The above is the detailed content of How to set up a floating window in bootstrap. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
What is Bootstrap? An Introduction for BeginnersWhat is Bootstrap? An Introduction for BeginnersApr 22, 2025 am 12:07 AM

BootstrapisafreeCSSframeworkthatsimplifieswebdevelopmentbyprovidingpre-styledcomponentsandJavaScriptplugins.It'sidealforcreatingresponsive,mobile-firstwebsites,offeringaflexiblegridsystemforlayoutsandasupportivecommunityforlearningandcustomization.

Bootstrap Demystified: A Simple ExplanationBootstrap Demystified: A Simple ExplanationApr 21, 2025 am 12:13 AM

Bootstrapisafree,open-sourceCSSframeworkthathelpscreateresponsive,mobile-firstwebsites.1)Itoffersagridsystemforlayoutflexibility,2)includespre-styledcomponentsforquickdesign,and3)ishighlycustomizabletoavoidgenericlooks,butrequiresunderstandingCSStoop

Bootstrap vs. React: Choosing the Right ApproachBootstrap vs. React: Choosing the Right ApproachApr 20, 2025 am 12:09 AM

Bootstrap is suitable for quick construction and small projects, while React is suitable for complex and interactive applications. 1) Bootstrap provides predefined CSS and JavaScript components to simplify responsive interface development. 2) React improves performance and interactivity through component development and virtual DOM.

Bootstrap's Purpose: Building Consistent and Attractive WebsitesBootstrap's Purpose: Building Consistent and Attractive WebsitesApr 19, 2025 am 12:07 AM

The main purpose of Bootstrap is to help developers quickly build responsive, mobile-first websites. Its core functions include: 1. Responsive design, which realizes layout adjustments of different devices through a grid system; 2. Predefined components, such as navigation bars and modal boxes, ensure aesthetics and cross-browser compatibility; 3. Support customization and extensions, and use Sass variables and mixins to adjust styles.

Bootstrap vs. Other Frameworks: A Comparative OverviewBootstrap vs. Other Frameworks: A Comparative OverviewApr 18, 2025 am 12:06 AM

Bootstrap is better than TailwindCSS, Foundation, and Bulma because it is easy to use and quickly develop responsive websites. 1.Bootstrap provides a rich library of predefined styles and components. 2. Its CSS and JavaScript libraries support responsive design and interactive functions. 3. Suitable for rapid development, but custom styles may be more complicated.

Integrating Bootstrap Styles in React: Methods and TechniquesIntegrating Bootstrap Styles in React: Methods and TechniquesApr 17, 2025 am 12:04 AM

Integrating Bootstrap in React projects can be done in two ways: 1) introduced using CDN, suitable for small projects or rapid prototyping; 2) installation using npm package manager, suitable for scenarios that require deep customization. With these methods, you can quickly build beautiful and responsive user interfaces in React.

Bootstrap in React: Advantages and Best PracticesBootstrap in React: Advantages and Best PracticesApr 16, 2025 am 12:17 AM

Advantages of integrating Bootstrap into React projects include: 1) rapid development, 2) consistency and maintainability, and 3) responsive design. By directly introducing CSS files or using the React-Bootstrap library, you can use Bootstrap's components and styles efficiently in your React project.

Bootstrap: A Quick Guide to Web FrameworksBootstrap: A Quick Guide to Web FrameworksApr 15, 2025 am 12:10 AM

Bootstrap is a framework developed by Twitter to help quickly build responsive, mobile-first websites and applications. 1. Ease of use and rich component libraries make development faster. 2. The huge community provides support and solutions. 3. Introduce and use class names to control styles through CDN, such as creating responsive grids. 4. Customizable styles and extension components. 5. Advantages include rapid development and responsive design, while disadvantages are style consistency and learning curve.

See all articles

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

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor