Showing posts with label coding. Show all posts
Showing posts with label coding. Show all posts

Thursday, December 22, 2022

CSS Transitions Tutorial for newbie

 CSS Styling

CSS3 has made the web a much more dynamic place with support for transitions and animations. CSS Animations like transitions is still developing as a standard, so in all examples we’re giving here you should add a prefix for each browsers and Firefox. Animation are used in abundance on the web, it’s only a matter of time. Previously even simple animations in HTML required complex JavaScript code, and most complex animations needed flash. Of course JavaScript is still incredibly useful if there has to be any form of interactivity, but new CSS3 features take a lot of the pain away. Let’s look at two CSS3 modules – Transitions and Animations. 

CSS Transitions

The “:hover” selector in CSS already made it possible to make a page layout dynamic without requiring any JavaScript. Essentially, the “:hover” selector can be used to define the CSS styling of an element when a mouse hovers over it. It can be used to change the font size, the font/background color, the border size/color type etc. Try this; create a HTML file with just the following in it – 

<! DOCTYPE html>


<html>


<head>


<title>Playing with CSS3</title>


<style type=”text/css”>


#div1


{


Background-color:blue;


}


#div2


{


Border: solid thin black;


}


#div1:hover


{


Background-color:red;


}


#div2:hover


{


Border: dashed thick black; 

}

#div3:hover


{


Font-size: 28px;


}


</style>


</head>


<body>


<div id=”div1”> Background color sample</div>


<div id=”div1”> Border sample</div>


<div id=”div1”> Font sample</div>


</body>


</html> 

If you save this in an HTML file you’ll see three rows. The first row is written in black on blue background, the second has a border, and the final one is just plain text. On moving your mouse over any row the formatting changes. The first row’s background becomes red, the second row’s border style changes and the third row’s font size increases. You may notice that this change is rather jerky. Now change the above styling for “div1 as follows – 

#div1


{


Background-color:blue;


-moz-transition-duration: 3s;


-webkit-transition-duration: 3s;


-o-transition-duration: 3s


Transition-duration: 3s;


When you hover your mouse over the first line, the color will smoothly go from blue to red. You’ll notice we have “transition-duration” multiple times with different prefixes. To know why this is needed because, each browser engine uses a different prefix; Mozilla’s Gecko engine in Firefox uses a prefix of “-moz-”, Opera uses “-o-”, and Microsoft’s Internet Explorer uses “-ms-” prefix. Since Safari and Chrome use the same engine (Webkit), their prefix is also the same “-webkit-“. Initially to use the same CSS feature on multiple browsers, you’ll need to define the same property multiple times – once for each browser. However, eventually the prefix will be dropped, and you will be able to use the same feature on all browsers. Since prefixes will be eventually dropped, it’s a good idea to define the property without a prefix as well, to ensure it continues working even when the prefix is dropped.

Now get back to the example here we have given you, in this example, all we gave was the duration of the transition, i.e. three seconds, and the browser automatically animated all changed properties – those that are possible to animate obviously. A number of CSS properties can be included in this transition, and it’s even possible to specify which properties to change, and durations and easing functions for each. Now look at the small example. Here on mouse hover we will change the Font and background color, and add a shadow. We will animate the background color change over two seconds and the shadow over 500 milliseconds, but we will not animate the Font color. Here is the relevant part of the code – 

<div id=”div1”> Just a simple sentence for testing purpose</div> 

CSS Part:


#div1


{


background-color: rgb(204, 204, 204);


transition-property: box-shadow, background-color;


transition-duration: 500ms, 2s;


}


#div1:hover


{


Box-shadow: 3px 3px 4px 1px black;


Color: rgb(255, 255, 255);


Background-color: rgb(102, 102, 102);


But you need to add proper prefix at transition property to make this code work properly, we have left it out to avoid redundancy in code.

After “transition-property” we have a comma separated list of properties that we wish to animate: after “transition-duration” we are providing the transition durations for each specified properties. If we had specified only one duration, it would be used for all properties. There are two more properties of transitions –




1) Transition-delay: you can use this to specify a delay before the animations is triggered.


2) Transition-timing-function: this function is used to calculate intermediate values between the two transition points. Predefined ones are ease, linear, ease-in, ease-out, ease-in-out. Or you can specify your own via cubic-bezier (a, b, c, d) where a, b, c, d are numbers.

Transitions need not to be triggered only on hover, if the CSS styles of an element change under other conditions, clicking for example, they will be animated if a transition is specified.

CSS Animations

Transition and animation share a lot of similar concepts, so let’s first look at the common elements. Just like transitions, animations have duration (“animation-duration”), a delay (“animation-delay”) and a timing function (“animation-timing-function”). Animations have two components – first, a description of the animation and second, a set of key-frames that define how the animation occurs over time. Animations are executed immediately on all elements matching their CSS selector. This might not be desirable if you wish to apply an animation when someone clicks on a Play Button. For that, you will need to use JavaScript to apply the animated class then required. Here is how you can specify an animation on a div with an id of animated – 

CSS Part:


#animated


{


animation-name: test-animation;


animation-duration: 4s;


animation-delay: 3s;


animation-iteration-count: 10;


animation-direction: alternate;


Here, we’re specifying an animation name, duration of four seconds, and a delay of three seconds. Via the iteration count we’re specifying that the animation has to run 10 times. The animation-direction property is set to “alternate” to specify that the animation must reverse when completed. Thus the animation will go forward and in the subsequent iteration will play backwards, and so on.

This of course, has said nothing of what the animation itself will look like. That we specify using the “@keyframes” keyword as follows – 

CSS Part:


@keyframes test-animation


{


from { width:10px; height: 4px;}


25% { width: 50px; height: 4px }


to { width: 50px; height: 200px }


That is. @keyframes followed by the name of the animation. These set of keyframes will accomplish the following –




1) In the beginning, the height and width of div will be set to four pixels, and 10 pixels respectively.


2) From the beginning to 25 percent of the way through the animation, i.e. after 1 second, the width will increase to 50 pixels, but the height will remain the same.


3) From 25% till the end of the animation, the width will remain the same, but the height will increase to 200 pixels.

Now let’s create a small animation of a red square moving between two points – 

CSS Part:


#animated


{


animation-name: test-animation;


animation-duration: 4s;


animation-delay: 3s;


animation-iteration-count: 10;


animation-direction: alternate;


animation-timing-function: linear;


background-color: red;


border: thin solid black;


width: 20px;


height: 20px;


position: absolute;


@keyframes test-animation


{


from { left: 0px;}


50% { left: 300px; width: 50px }


to { left: 600px;}


This will bounce the square between 0px and 600px, while increasing the width to 50px in the middle. CSS Animation like transitions, is still developing as a standard, a standard in only useful if it is implemented, however, standards are often implemented even before they’re fully defined.


SQL Server Cache Dependency

 Optimization

Building website is not enough, as everything depends on its performance. If you have developed a good looking site with lots of data handling but your website is taking higher response time, then it’s all senseless having such website even if you are handling data smartly. Here comes the role of optimization process, it plays crucial role behind the success of your website. And this dependency becomes higher if you are not using prebuild CMS like WordPress or DotNetNuke. There are many ways to optimize your website contents like DataCaching, Network Caching, and SQL Server Caching etc. Today, we are going to explain SQL Server Cache using in ASP.NET Web Application with MS SQL Server 2005.

SQL Server Cache Dependency

It’s one of the most complex caching mechanisms available for .net applications, it provides functionality to invalidate data object whenever a change occurs within the related table of Database. For asp.net application we can consider it as the best possible mechanisms for caching data. In MS SQL Server 2005 and later versions this message system is built with Service Broker. The Service Broker manages queues, which are database objects that have the same standing as tables, stored procedure, or view.

To build Cache Dependency you need to enable Service Broker on each database where you want to use it. This service can be enabled easily with the help of Visual Studio Command Tool by executing the following command – 

SqlCmd –S .\SQLEXPRESS 

USE database_name


ALTER DATABASE database_name SET ENABLE_BROKER


Go 

Now you need to enable Subscribe Query Notification for this database to receive messages, type the following code to do so – 

GRANT SUBSCRIBE QUERY NOTIFICATIONS TO database_name 

To check all the subscriptions those are currently stored into the database use this command – 

SELECT * FROM sys.dm_qn_subscriptions 

You can also kill a subscription using -

KILL QUERY NOTIFICATION SUBSCRIPTION id 

Where ‘id’ is the subscription id, you can kill all subscription using ‘All’ keyword in place of subscription id. Now we have to enable SQL Cache Dependency on your database with the following command – 

aspnet_regsql -d -E -d database_name 

Here –




1) –d: Specifies the name of the database to create or modify for use with application services. If the database is not specified, the default database name of “aspnetdb” is used.

2) –E: Authenticates using the Windows credentials of the currently logged-on user.

3) –ed: Enables a database for SQL cache dependency.

Now we have to specify the particular table name for enabling cache on that’s data by executing this command – 

aspnet_regsql -et -E -d database_name -t table_name 

Here –




1) – t: Specifies the table name to enable or disable for use with SQL cache dependency. This option must be used with the -et or -dt options.

2) – et: Enables a table for SQL cache dependency. The -t option must also be included in the parameter string.

Now move on to ‘web.config’ file of your application and add the following parameters to make SqlCacheDependency work – 

<connectionStrings>


<clear />


<add name=”LocalSqlServer”


connectionString=”data source=.\SQLEXPRESS;


Integrated Security=SSPI;


initial catalog=database_name” />


</connectionStrings> 

<system.web>


<caching>


<sqlCacheDependency pollTime=”1000″ enabled=”true” >


<databases>


<add connectionStringName=”LocalSqlServer” name=”database_name”/>


</databases>


</sqlCacheDependency>


</caching>


</system.web> 

In above code we set PollTime to 1000 milliseconds that means after every 10 seconds database will be checked for any changes in particular table. Now take a look how to use SqlCacheDependency in asp.net code – 

private void BindData()


{


// if null then fetch from the database 

if (Cache["any_name"] == null)


{


// Create the cache dependency

SqlCacheDependency dep = new SqlCacheDependency(“database_name”, “table_name”);


string connectionString = ConfigurationManager.ConnectionStrings[


"LocalSqlServer"].ConnectionString;


SqlConnection myConnection = new SqlConnection(connectionString);


SqlDataAdapter ad = new SqlDataAdapter(“Your Query”, myConnection);


DataSet ds = new DataSet();


ad.Fill(ds);

// put in the cache object

Cache.Insert(“any_name”, ds, dep);


}

// use any data control to bind with cached object

GridView1.DataSource = (DataSet) Cache["any_name"] ;


GridView1.DataBind();


Here first we checked whether cache object is null or have any data, if it has data then we can bind our data control (in this example Grid View) with this cache item, else we will create a SqlCacheDependency object by passing parameters of database name and table name on which we enabled the SQL Cache Dependency. Then after took the connection string value set in the web.config file and executed our query and got the returned data into Data Set object. Then we insert this data set object into cache object with the parameters of its name, data set object and the SqlCacheDependency object we created earlier and bound the Grid View with this cache object.

Note – 

We cannot use Data Reader object to implement SqlCacheDependency in our asp.net application, because it uses an active SQL connection unlike Data Set which works with disconnected environment. 


The Memory Revolution: Understanding the Impact of DDR5 Technology on the Industry

The Memory Revolution: Understanding the Impact of DDR5 Technology on the Industry - DDR5 (Double Data Rate 5) is the most recent generation...